]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Quash bogus -Wunused-value with new [PR107797]
authorMarek Polacek <polacek@redhat.com>
Thu, 19 Jan 2023 22:12:34 +0000 (17:12 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 23 Jan 2023 21:41:48 +0000 (16:41 -0500)
We shouldn't emit "right operand of comma operator has no effect"
when that comma operator was created by the compiler for "new int{}".
convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
we can just suppress -Wunused-value.

PR c++/107797

gcc/cp/ChangeLog:

* cvt.cc (ocp_convert): copy_warning when creating a new
COMPOUND_EXPR.
* init.cc (build_new_1): Suppress -Wunused-value on
compiler-generated COMPOUND_EXPRs.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wunused-value-1.C: New test.

gcc/cp/cvt.cc
gcc/cp/init.cc
gcc/testsuite/g++.dg/warn/Wunused-value-1.C [new file with mode: 0644]

index 0cbfd8060cb3b698a96f7baec0ac619b1f5f3679..17827d06a4a6693e27710394c889756e8765a288 100644 (file)
@@ -711,8 +711,10 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
        return error_mark_node;
       if (e == TREE_OPERAND (expr, 1))
        return expr;
-      return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
-                        TREE_OPERAND (expr, 0), e);
+      e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
+                     TREE_OPERAND (expr, 0), e);
+      copy_warning (e, expr);
+      return e;
     }
 
   complete_type (type);
index f816c474cef7719c7dbc6bbc888b95510c822366..52e96fbe59088f7e8043e76a5969612a7c75309a 100644 (file)
@@ -3800,6 +3800,8 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
   if (cookie_expr)
     rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
 
+  suppress_warning (rval, OPT_Wunused_value);
+
   if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
     /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
        and return the call (which doesn't need to be adjusted).  */
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-value-1.C b/gcc/testsuite/g++.dg/warn/Wunused-value-1.C
new file mode 100644 (file)
index 0000000..2ba5587
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/107797
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused" }
+
+void
+g ()
+{
+  (long) new int{};
+  long(new int{});
+  (long) new int();
+  long(new int());
+}