From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 127C5385842E; Mon, 24 Jan 2022 09:21:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 127C5385842E MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-9501] c++: Silence -Wuseless-cast warnings during move [PR103480] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 777b73e45983b11e010bd5192185ad01af444de4 X-Git-Newrev: cb4998fcdf2ad92fc6323fc4d9bc03299ca8a541 Message-Id: <20220124092123.127C5385842E@sourceware.org> Date: Mon, 24 Jan 2022 09:21:23 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Jan 2022 09:21:23 -0000 https://gcc.gnu.org/g:cb4998fcdf2ad92fc6323fc4d9bc03299ca8a541 commit r11-9501-gcb4998fcdf2ad92fc6323fc4d9bc03299ca8a541 Author: Jakub Jelinek 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 Jason Merrill 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. (cherry picked from commit 6bba184ccbf47368eaea27ee2c1e7b850526640b) 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 5c445b51e3f..306f61f719e 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -1275,6 +1275,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 +struct A { typedef T t[N]; }; +template +struct B { typename A::t b; }; +struct C { + constexpr C (C &&) {} + template + static auto bar () + { + B r; + return r; // { dg-bogus "useless cast to type" } + } + C () = default; +}; + +void +foo () +{ + C::bar<2> (); +}