-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Open
Labels
Description
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.