On Tue, 16 Jul 2024, 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's surprising, because even the C FE emits a -Wunused-value
warning for '!f();'.  Other compilers also diagnose it.

The standard way to silence the warning, which works even for
non-integral expressions, is to just cast to void I think.

See https://gcc.gnu.org/PR114104#c1 for a realistic bug that we would
now diagnose with this patch.

> 
> >         PR c++/114104
> >
> > gcc/cp/ChangeLog:
> >
> >         * cvt.cc (convert_to_void): Call warn_if_unused_value for
> >         TRUTH_NOT_EXPR as well.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * g++.dg/warn/Wunused-20.C: New test.
> > ---
> >  gcc/cp/cvt.cc                          |  1 +
> >  gcc/testsuite/g++.dg/warn/Wunused-20.C | 17 +++++++++++++++++
> >  2 files changed, 18 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..ebff6db2ea9 100644
> > --- a/gcc/cp/cvt.cc
> > +++ b/gcc/cp/cvt.cc
> > @@ -1664,6 +1664,7 @@ 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 == 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..6d7838fab13
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/warn/Wunused-20.C
> > @@ -0,0 +1,17 @@
> > +// PR c++/114104
> > +// { dg-additional-options "-Wunused-value" }
> > +
> > +bool f();
> > +
> > +void g() {
> > +  !f(); // { dg-warning "value computed is not used" }
> > +}
> > +
> > +#if  __cplusplus >= 202002L
> > +struct A { };
> > +[[nodiscard]] bool operator==(A, int);
> > +
> > +void g(A a) {
> > +  a != 0; // { dg-warning "value computed is not used" "" { target c++20 } 
> > }
> > +}
> > +#endif
> > --
> > 2.46.0.rc0.35.gad850ef1cf
> >
> 
> 

Reply via email to