public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-6516] c++: Silence -Wuseless-cast warnings during move [PR103480]
Date: Wed, 12 Jan 2022 08:49:21 +0000 (GMT)	[thread overview]
Message-ID: <20220112084921.847D83858001@sourceware.org> (raw)

https://gcc.gnu.org/g:6bba184ccbf47368eaea27ee2c1e7b850526640b

commit r12-6516-g6bba184ccbf47368eaea27ee2c1e7b850526640b
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 12 09:47:46 2022 +0100

    c++: Silence -Wuseless-cast warnings during move [PR103480]
    
    This is maybe just a shot in the dark, but IMHO we shouldn't be diagnosing
    -Wuseless-cast on casts the compiler adds on its own when calling its move
    function.  We don't seem to warn when user calls std::move either.
    We call move on elinit (*NON_LVALUE_EXPR <(struct C[2] &&) &D.2497->b>)[0]
    so it is already an xvalue_p and try to static_cast it to struct C &&.
    But we don't warn e.g. on std::move (std::move (whatever)).
    
    Fixed by not doing the static cast and just returning expr from move
    if expr is already an xvalue.
    
    2022-01-11  Jakub Jelinek  <jakub@redhat.com>
                Jason Merrill  <jason@redhat.com>
    
            PR c++/103480
            * tree.c (move): If expr is xvalue_p, just return expr without
            build_static_cast.
    
            * g++.dg/warn/Wuseless-cast2.C: New test.

Diff:
---
 gcc/cp/tree.c                              |  2 ++
 gcc/testsuite/g++.dg/warn/Wuseless-cast2.C | 24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index d0c6490e42f..7f7de86b4e8 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -1304,6 +1304,8 @@ move (tree expr)
 {
   tree type = TREE_TYPE (expr);
   gcc_assert (!TYPE_REF_P (type));
+  if (xvalue_p (expr))
+    return expr;
   type = cp_build_reference_type (type, /*rval*/true);
   return build_static_cast (input_location, type, expr,
 			    tf_warning_or_error);
diff --git a/gcc/testsuite/g++.dg/warn/Wuseless-cast2.C b/gcc/testsuite/g++.dg/warn/Wuseless-cast2.C
new file mode 100644
index 00000000000..22e403973e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wuseless-cast2.C
@@ -0,0 +1,24 @@
+// PR c++/103480
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wuseless-cast" }
+
+template <typename T, int N>
+struct A { typedef T t[N]; };
+template <typename T, int N>
+struct B { typename A<T, N>::t b; };
+struct C {
+  constexpr C (C &&) {}
+  template <int N>
+  static auto bar ()
+  {
+    B<C, N> r;
+    return r;		// { dg-bogus "useless cast to type" }
+  }
+  C () = default;
+};
+
+void
+foo ()
+{
+  C::bar<2> ();
+}


                 reply	other threads:[~2022-01-12  8:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220112084921.847D83858001@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /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).