https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86982
Bug ID: 86982 Summary: Make -Wreturn-local-addr know about std::move and std::forward Product: gcc Version: 9.0 Status: UNCONFIRMED Keywords: diagnostic Severity: enhancement Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- This fails to warn: #include <utility> int&& f() { int i = 0; return std::move(i); } The equivalent code using a static_cast to rvalue reference warns: move.cc: In function 'int&& f2()': move.cc:3:52: warning: reference to local variable 'i' returned [-Wreturn-local-addr] 3 | int&& f2() { int i = 0; return static_cast<int&&>(i); } | ^ move.cc:3:18: note: declared here 3 | int&& f2() { int i = 0; return static_cast<int&&>(i); } | ^ This leads to bugs in users' code, like PR 86980. As std::move is so fundamental to modern C++ I think it deserves special treatment by the front end, to ensure this warns. (The special handling might be useful for implementing PR 86981 as well). It would be great to do the same for std::forward, which has the same problem: #include <utility> int&& f() { int i = 0; return std::move(i); } int&& g() { int i = 0; return std::forward<int>(i); } std::forward can also be used to return references to temporaries: int&& h() { long l = 0; return std::forward<int>(l); } Again, the equivalent with a static_cast gives a warning: move.cc: In function 'int&& h2()': move.cc:5:53: warning: returning reference to temporary [-Wreturn-local-addr] 5 | int&& h2() { long l = 0; return static_cast<int&&>(l); } |