public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: Stephan Bergmann <sbergman@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts
Date: Mon, 22 Aug 2022 17:02:12 -0400	[thread overview]
Message-ID: <YwPu1O8B7HPMth+h@redhat.com> (raw)
In-Reply-To: <b03beefc-0a45-fcec-b370-8e53bed7ff54@redhat.com>

On Mon, Aug 22, 2022 at 01:48:34PM +0200, Stephan Bergmann wrote:
> On 16/08/2022 14:27, Marek Polacek via Gcc-patches wrote:
> > Ping.  (The other std::move patches depend on this one.)
> 
> <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=8d22c7cb8b1a6f9b67c54a798dd5504244614e51>
> "c++: Extend -Wpessimizing-move to other contexts" started to cause false
> positive

Thanks for reporting the problem.  I'm testing a simple patch (appended
below) and will post it once testing finishes.
 
> > $ cat test.cc
> > #include <utility>
> > struct S1 {
> >     S1();
> >     S1(S1 const &) = delete;
> >     S1(S1 &&);
> >     S1 operator =(S1 const &) = delete;
> >     S1 operator =(S1 &&);
> > };
> > struct S2 { S2(S1); };
> > S2 f() {
> >     S1 s;
> >     return { std::move(s) };
> > }
> > 
> > $ g++ -fsyntax-only -Wredundant-move test.cc
> > test.cc: In function ‘S2 f()’:
> > test.cc:12:27: warning: redundant move in return statement [-Wredundant-move]
> >    12 |     return { std::move(s) };
> >       |                           ^
> > test.cc:12:27: note: remove ‘std::move’ call

--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -10447,7 +10447,7 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
     return;

   /* A a = std::move (A());  */
-  if (TREE_CODE (expr) == TREE_LIST)
+  if (TREE_CODE (expr) == TREE_LIST && !return_p)
     {
       if (list_length (expr) == 1)
    expr = TREE_VALUE (expr);
@@ -10456,7 +10456,7 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
     }
   /* A a = {std::move (A())};
      A a{std::move (A())};  */
-  else if (TREE_CODE (expr) == CONSTRUCTOR)
+  else if (TREE_CODE (expr) == CONSTRUCTOR && !return_p)
     {
       if (CONSTRUCTOR_NELTS (expr) == 1)
    expr = CONSTRUCTOR_ELT (expr, 0)->value;

Marek


  reply	other threads:[~2022-08-22 21:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02 23:04 Marek Polacek
2022-08-16 12:27 ` Marek Polacek
2022-08-22 11:48   ` Stephan Bergmann
2022-08-22 21:02     ` Marek Polacek [this message]
2022-08-16 19:23 ` Jason Merrill
2022-08-16 21:09   ` Marek Polacek
2022-08-17 16:11     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YwPu1O8B7HPMth+h@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=sbergman@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).