Skip to content

bugprone-move-forwarding-reference: false positive for move only types #154908

@Dushistov

Description

@Dushistov

For such code:

 #include <memory>

  void bar(std::unique_ptr<int> t) {}

  template <typename T> void foo(T &&t) { bar(std::move(t)); }

  int main() {
    std::unique_ptr<int> s = std::make_unique<int>(17);
    foo(s);
  }

bugprone-move-forwarding-reference suggets to use std::forward:

clang-tidy test.cpp \
  -checks='bugprone-move-forwarding-reference' \
  -header-filter='.*' \
  -- -std=c++17
1 warning generated.
/tmp/test.cpp:5:45: warning: forwarding reference passed to std::move(), which may unexpectedly cause lvalues to be moved; use std::forward() instead [bugprone-move-forwarding-reference]
    5 | template <typename T> void foo(T &&t) { bar(std::move(t)); }
      |                                             ^~~~~~~~~
      |                                             std::forward<T>

but because of std::unqiue_ptr is move only type and bar take it by value, usage of this suggestion just cause compilation error:

test.cpp:5:45: error: no matching function for call to 'forward'                                                                                                                           
    5 | template <typename T> void foo(T &&t) { bar(std::forward(t)); }                                                                                                                    
      |                                             ^~~~~~~~~~~~         

so it would be nice, if bugprone-move-forwarding-reference check verifing is type if move only and if it is accepted by value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions