https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97748
--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Untested WIP patch, though C++ FE needs some extra work: --- gcc/c-family/c-common.h.jj 2020-11-03 11:15:07.170681001 +0100 +++ gcc/c-family/c-common.h 2020-11-07 09:37:48.597233063 +0100 @@ -1362,7 +1362,7 @@ extern void warn_tautological_cmp (const tree, tree); extern void warn_logical_not_parentheses (location_t, enum tree_code, tree, tree); -extern bool warn_if_unused_value (const_tree, location_t); +extern bool warn_if_unused_value (const_tree, location_t, bool = false); extern bool strict_aliasing_warning (location_t, tree, tree); extern void sizeof_pointer_memaccess_warning (location_t *, tree, vec<tree, va_gc> *, tree *, --- gcc/c-family/c-warn.c.jj 2020-10-26 10:53:56.533885147 +0100 +++ gcc/c-family/c-warn.c 2020-11-07 09:40:51.011170825 +0100 @@ -585,7 +585,7 @@ warn_logical_not_parentheses (location_t (potential) location of the expression. */ bool -warn_if_unused_value (const_tree exp, location_t locus) +warn_if_unused_value (const_tree exp, location_t locus, bool quiet) { restart: if (TREE_USED (exp) || TREE_NO_WARNING (exp)) @@ -633,7 +633,7 @@ warn_if_unused_value (const_tree exp, lo goto restart; case COMPOUND_EXPR: - if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) + if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, quiet)) return true; /* Let people do `(foo (), 0)' without a warning. */ if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) @@ -648,6 +648,13 @@ warn_if_unused_value (const_tree exp, lo return false; goto warn; + case COMPLEX_EXPR: + /* Warn only if both operands are unused. */ + if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus, true) + && warn_if_unused_value (TREE_OPERAND (exp, 1), locus, true)) + goto warn; + return false; + case INDIRECT_REF: /* Don't warn about automatic dereferencing of references, since the user cannot control it. */ @@ -671,6 +678,8 @@ warn_if_unused_value (const_tree exp, lo return false; warn: + if (quiet) + return true; return warning_at (locus, OPT_Wunused_value, "value computed is not used"); } } --- gcc/testsuite/c-c++-common/Wunused-value-1.c.jj 2020-11-07 09:51:46.407757100 +0100 +++ gcc/testsuite/c-c++-common/Wunused-value-1.c 2020-11-07 10:15:34.295574782 +0100 @@ -0,0 +1,33 @@ +/* PR c/97748 */ +/* { dg-do compile } */ +/* { dg-options "-Wunused-value" } */ + +double _Complex f (); +double _Complex *p; + +double _Complex +foo (double _Complex x) +{ + ++x; /* { dg-bogus "value computed is not used" } */ + --x; /* { dg-bogus "value computed is not used" } */ + x += 1; /* { dg-bogus "value computed is not used" } */ + x += 1.0iF; /* { dg-bogus "value computed is not used" } */ + x++; /* { dg-bogus "value computed is not used" } */ + x--; /* { dg-bogus "value computed is not used" } */ + x + 1; /* { dg-warning "value computed is not used" } */ + (void) (x + 1); /* { dg-bogus "value computed is not used" } */ + 1 + f (); /* { dg-warning "value computed is not used" } */ + f () + f (); /* { dg-warning "value computed is not used" } */ + f () + f (), f (); /* { dg-warning "value computed is not used" } */ + f (); + (void) f (); + *p++; /* { dg-warning "value computed is not used" } */ + ++*p; /* { dg-bogus "value computed is not used" } */ + (*p ? f () : 0); + ({ f (); }); + ({ f () + 1; }); + ({ f (); 0; }); + ({ f () + 1; 0; }); /* { dg-warning "value computed is not used" } */ + 1 + ({ f (); }); /* { dg-warning "value computed is not used" } */ + return x; +}