c++: Extend -Wpessimizing-move to other contexts
commit8d22c7cb8b1a6f9b67c54a798dd5504244614e51
authorMarek Polacek <polacek@redhat.com>
Mon, 1 Aug 2022 21:02:23 +0000 (1 17:02 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 17 Aug 2022 16:35:45 +0000 (17 12:35 -0400)
tree1c20cb245dd1cdb66727bde28f86ce9850e5e056
parent745be54bd6634fe63d6be83615e264c83d2ae9f9
c++: Extend -Wpessimizing-move to other contexts

In my recent patch which enhanced -Wpessimizing-move so that it warns
about class prvalues too I said that I'd like to extend it so that it
warns in more contexts where a std::move can prevent copy elision, such
as:

  T t = std::move(T());
  T t(std::move(T()));
  T t{std::move(T())};
  T t = {std::move(T())};
  void foo (T);
  foo (std::move(T()));

This patch does that by adding two maybe_warn_pessimizing_move calls.
These must happen before we've converted the initializers otherwise the
std::move will be buried in a TARGET_EXPR.

PR c++/106276

gcc/cp/ChangeLog:

* call.cc (build_over_call): Call maybe_warn_pessimizing_move.
* cp-tree.h (maybe_warn_pessimizing_move): Declare.
* decl.cc (build_aggr_init_full_exprs): Call
maybe_warn_pessimizing_move.
* typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
message.
(check_return_expr): Adjust the call to maybe_warn_pessimizing_move.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
* g++.dg/cpp0x/Wpessimizing-move8.C: New test.
gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C
gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C [new file with mode: 0644]