c++: Extend -Wredundant-move for const-qual objects [PR90428]
commit6c136d53e89e32fe9d22e41e5df89c08d3470049
authorMarek Polacek <polacek@redhat.com>
Wed, 17 Aug 2022 17:36:52 +0000 (17 13:36 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 17 Aug 2022 17:36:52 +0000 (17 13:36 -0400)
tree99a2a10d5bd94b903952239db63a15fe0a3c4201
parent6602a2b2dee16af6e2d451c704789356042b5881
c++: Extend -Wredundant-move for const-qual objects [PR90428]

In this PR, Jon suggested extending the -Wredundant-move warning
to warn when the user is moving a const object as in:

  struct T { };

  T f(const T& t)
  {
    return std::move(t);
  }

where the std::move is redundant, because T does not have
a T(const T&&) constructor (which is very unlikely).  Even with
the std::move, T(T&&) would not be used because it would mean
losing the const.  Instead, T(const T&) will be called.

I had to restructure the function a bit, but it's better now.  This patch
depends on my other recent patches to maybe_warn_pessimizing_move.

PR c++/90428

gcc/cp/ChangeLog:

* typeck.cc (can_do_rvo_p): Rename to ...
(can_elide_copy_prvalue_p): ... this.
(maybe_warn_pessimizing_move): Extend the
-Wredundant-move warning to warn about std::move on a
const-qualified object.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/Wredundant-move1.C: Adjust dg-warning.
* g++.dg/cpp0x/Wredundant-move9.C: Likewise.
* g++.dg/cpp0x/Wredundant-move10.C: New test.
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/Wredundant-move1.C
gcc/testsuite/g++.dg/cpp0x/Wredundant-move10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/Wredundant-move9.C