On Tue, 16 Jul 2024, Jason Merrill wrote: > On 7/16/24 10:31 AM, Eric Gallager wrote: > > On Mon, Jul 15, 2024 at 10:37 PM Patrick Palka <ppa...@redhat.com> wrote: > > > > > > Bootstrapped andrregtested on x86_64-pc-linux-gnu, does this look > > > OK for trunk? > > > > > > -- >8 -- > > > > > > Here we're neglecting to emit a -Wunused-value for eligible ! 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 > > > consider warning for TRUTH_NOT_EXPR as well. > > > > > > > Eh, I think I've seen the ! operator recommended as a way to silence > > -Wunused-value previously in cases where an actual fix isn't > > practical; some people might be mad about this... > > That sounds like weird advice to me; I'd expect the result of ! to be used, > and we already warn if the operand is something other than bool. Clang also > warns for the bool case. > > Seems like ADDR_EXPR is another tcc_expression that could use handling here, > e.g. > > struct A { int i; }; > A& f(); > int main() { > &f().i; // missed warning > }
Makes sense, like so? Bootstrapped and regtested on x86_64-pc-linux-gnu -- >8 -- Subject: [PATCH] c++: missing -Wunused-value for !<expr> [PR114104] Here we're neglecting to emit a -Wunused-value for eligible ! 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 consider warning for TRUTH_NOT_EXPR as well, along with 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. --- gcc/cp/cvt.cc | 2 ++ gcc/testsuite/g++.dg/warn/Wunused-20.C | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-20.C diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc index db086c017e8..d95e01c118c 100644 --- a/gcc/cp/cvt.cc +++ b/gcc/cp/cvt.cc @@ -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 index 00000000000..31b1920adcd --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-20.C @@ -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 -- 2.46.0.rc0.75.g04f5a52757