PR c++/87378 - bogus -Wredundant-move warning.
authorMarek Polacek <polacek@redhat.com>
Wed, 6 Mar 2019 15:34:50 +0000 (15:34 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 6 Mar 2019 15:34:50 +0000 (15:34 +0000)
* typeck.c (maybe_warn_pessimizing_move): See if the maybe-rvalue
overload resolution would actually succeed.

* g++.dg/cpp0x/Wredundant-move1.C (fn4): Drop dg-warning.
* g++.dg/cpp0x/Wredundant-move7.C: New test.

From-SVN: r269427

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/Wredundant-move1.C
gcc/testsuite/g++.dg/cpp0x/Wredundant-move7.C [new file with mode: 0644]

index d2cb7fd1132d78357d3b18afc9c0b0113e2ebcfc..cfd86b250d7401a9fd79f33180c350d9011b3d32 100644 (file)
@@ -1,3 +1,9 @@
+2019-03-06  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/87378 - bogus -Wredundant-move warning.
+       * typeck.c (maybe_warn_pessimizing_move): See if the maybe-rvalue
+       overload resolution would actually succeed.
+
 2019-03-05  Jason Merrill  <jason@redhat.com>
 
        * class.c (is_really_empty_class): Add ignore_vptr parm.
index 1bf9ad88141b6c6a7970a4d0472f2e489e68d3d4..43ff3d63abd592f8b7ba87677e4138ea7f7c269b 100644 (file)
@@ -9429,10 +9429,24 @@ maybe_warn_pessimizing_move (tree retval, tree functype)
             do maybe-rvalue overload resolution even without std::move.  */
          else if (treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true))
            {
-             auto_diagnostic_group d;
-             if (warning_at (loc, OPT_Wredundant_move,
-                             "redundant move in return statement"))
-               inform (loc, "remove %<std::move%> call");
+             /* Make sure that the overload resolution would actually succeed
+                if we removed the std::move call.  */
+             tree t = convert_for_initialization (NULL_TREE, functype,
+                                                  move (arg),
+                                                  (LOOKUP_NORMAL
+                                                   | LOOKUP_ONLYCONVERTING
+                                                   | LOOKUP_PREFER_RVALUE),
+                                                  ICR_RETURN, NULL_TREE, 0,
+                                                  tf_none);
+             /* If this worked, implicit rvalue would work, so the call to
+                std::move is redundant.  */
+             if (t != error_mark_node)
+               {
+                 auto_diagnostic_group d;
+                 if (warning_at (loc, OPT_Wredundant_move,
+                                 "redundant move in return statement"))
+                   inform (loc, "remove %<std::move%> call");
+               }
            }
        }
     }
index 828ed02d93acb57f1303734cca1a725911277e70..9b2951f631fb194ca79679aa83567cfd4abcfff2 100644 (file)
@@ -1,3 +1,9 @@
+2019-03-06  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/87378 - bogus -Wredundant-move warning.
+       * g++.dg/cpp0x/Wredundant-move1.C (fn4): Drop dg-warning.
+       * g++.dg/cpp0x/Wredundant-move7.C: New test.
+
 2019-03-06  Richard Biener  <rguenther@suse.de>
 
        PR testsuite/89551
index 5d4a25dbc3b55539ca8d6f7020f9929ef0336acd..e70f3cde625bb95f22a25c3bb55ee39212127a69 100644 (file)
@@ -59,7 +59,8 @@ T
 fn4 (const T t)
 {
   // t is const: will decay into copy despite std::move, so it's redundant.
-  return std::move (t); // { dg-warning "redundant move in return statement" }
+  // We used to warn about this, but no longer since c++/87378.
+  return std::move (t);
 }
 
 int
diff --git a/gcc/testsuite/g++.dg/cpp0x/Wredundant-move7.C b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move7.C
new file mode 100644 (file)
index 0000000..015d7c4
--- /dev/null
@@ -0,0 +1,59 @@
+// PR c++/87378
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wredundant-move" }
+
+// Define std::move.
+namespace std {
+  template<typename _Tp>
+    struct remove_reference
+    { typedef _Tp   type; };
+
+  template<typename _Tp>
+    struct remove_reference<_Tp&>
+    { typedef _Tp   type; };
+
+  template<typename _Tp>
+    struct remove_reference<_Tp&&>
+    { typedef _Tp   type; };
+
+  template<typename _Tp>
+    constexpr typename std::remove_reference<_Tp>::type&&
+    move(_Tp&& __t) noexcept
+    { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
+}
+
+struct S1 { S1(S1 &&); };
+struct S2 : S1 {};
+
+S1
+f (S2 s)
+{
+  return std::move(s); // { dg-bogus "redundant move in return statement" }
+}
+
+struct R1 {
+  R1(R1 &&);
+  R1(const R1 &&);
+};
+struct R2 : R1 {};
+
+R1
+f2 (const R2 s)
+{
+  return std::move(s); // { dg-bogus "redundant move in return statement" }
+}
+
+struct T1 {
+  T1(const T1 &);
+  T1(T1 &&);
+  T1(const T1 &&);
+};
+struct T2 : T1 {};
+
+T1
+f3 (const T2 s)
+{
+  // Without std::move: const T1 &
+  // With std::move: const T1 &&
+  return std::move(s); // { dg-bogus "redundant move in return statement" }
+}