https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106276

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpola...@gcc.gnu.org>:

https://gcc.gnu.org/g:ae3459cd5956fcd29e8aa5408efb707cb7d9b14f

commit r13-2045-gae3459cd5956fcd29e8aa5408efb707cb7d9b14f
Author: Marek Polacek <pola...@redhat.com>
Date:   Fri Jul 22 18:10:30 2022 -0400

    c++: Extend -Wpessimizing-move for class prvalues [PR106276]

    We already have a warning that warns about pessimizing std::move
    in a return statement, when it prevents the NRVO:

      T fn()
      {
        T t;
        return std::move (t); // warning \o/
      }

    However, the warning doesn't warn when what we are returning is a class
    prvalue, that is, when std::move prevents the RVO:

      T fn()
      {
        T t;
        return std::move (T{}); // no warning :-(
      }

    This came up recently in GCC:
    <https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598177.html>.

    This patch fixes that.  I would like to extend the warning further, so
    that it warns in more contexts, e.g.:

      T t = std::move(T());

    or

      void foo (T);
      foo (std::move(T()));

            PR c++/106276

    gcc/cp/ChangeLog:

            * typeck.cc (can_do_rvo_p): New.
            (maybe_warn_pessimizing_move): Warn when moving a temporary object
            in a return statement prevents copy elision.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/Wpessimizing-move7.C: New test.

Reply via email to