]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: missing -Wunused-value for !<expr> [PR114104]
authorPatrick Palka <ppalka@redhat.com>
Thu, 18 Jul 2024 00:57:54 +0000 (20:57 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 18 Jul 2024 00:57:54 +0000 (20:57 -0400)
Here we're neglecting to issue a -Wunused-value warning for suitable !
operator expressions, and in turn for != operator expressions that are
rewritten as !(x == y), only because we don't call warn_if_unused_value
on TRUTH_NOT_EXPR since its class is tcc_expression.  This patch makes
us also consider warning for TRUTH_NOT_EXPR and also for ADDR_EXPR.

PR c++/114104

gcc/cp/ChangeLog:

* cvt.cc (convert_to_void): Call warn_if_unused_value for
TRUTH_NOT_EXPR and ADDR_EXPR as well.

gcc/testsuite/ChangeLog:

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

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cvt.cc
gcc/testsuite/g++.dg/warn/Wunused-20.C [new file with mode: 0644]

index db086c017e893858ef5027c20c8a6357f9c79ac3..d95e01c118c4d8914a49f4b4455b1dd343f0545d 100644 (file)
@@ -1664,6 +1664,8 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
              if (tclass == tcc_comparison
                  || tclass == tcc_unary
                  || tclass == tcc_binary
+                 || code == TRUTH_NOT_EXPR
+                 || code == ADDR_EXPR
                  || code == VEC_PERM_EXPR
                  || code == VEC_COND_EXPR)
                warn_if_unused_value (e, loc);
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-20.C b/gcc/testsuite/g++.dg/warn/Wunused-20.C
new file mode 100644 (file)
index 0000000..31b1920
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/114104
+// { dg-additional-options "-Wunused-value" }
+
+bool f();
+struct A { int i; };
+A& g();
+
+void h() {
+  !f(); // { dg-warning "value computed is not used" }
+  &g().i; // { dg-warning "value computed is not used" }
+}
+
+#if  __cplusplus >= 202002L
+[[nodiscard]] bool operator==(A&, int);
+
+void h(A a) {
+  a != 0; // { dg-warning "value computed is not used" "" { target c++20 } }
+}
+#endif