c++: missing -Wunused-value for !<expr> [PR114104]

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>
This commit is contained in:
Patrick Palka 2024-07-17 20:57:54 -04:00
parent 313afcfdab
commit 144b6099cd
2 changed files with 21 additions and 0 deletions

View 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);

View File

@ -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