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.
{
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);
--- /dev/null
+// 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> ();
+}