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>
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);
--- /dev/null
+// 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