[PATCH] c, c++: Extend -Wunused-but-set-* warnings [PR44677]

Jakub Jelinek jakub@redhat.com
Tue Jul 15 09:26:01 GMT 2025


On Mon, Jul 14, 2025 at 11:58:32PM -0400, Jason Merrill wrote:
> Coming back to this comment, it still seems to me that we can generalize
> this and ignore anything cast to void here, as in the below; after something
> has been cast to void, it can no longer be read.  I don't get any
> regressions from this simplification, either.
> 
> We might generalize to anything of void type, but I haven't tested that.

We do want to warn for b and not for a e.g. on -Wunused-but-set-variable
void
foo ()
{
  int a, b;
  a = 1;
  b = 2;
  (void) a;
}
Though, from what I can see, we warn correctly even with your patch.
I admit I don't know any longer what was the reason for all the special
cases in mark_exp_read and whether over the years some of them might be
unneeded.  We have some test coverage, but only limited (40KB of tests
in c-c++-common & g++.dg), plus sure full bootstrap/regtest does test it
further.

Could it be committed separately though, in case there is some regression
that bisection can find whether it is this simplification or the original
patch?

> commit adcf4220b73a9b7f44a35728f60aa5b351ef51d8
> Author: Jason Merrill <jason@redhat.com>
> Date:   Mon Jul 14 18:29:17 2025 -0400
> 
>     void
> 
> diff --git a/gcc/cp/expr.cc b/gcc/cp/expr.cc
> index 8b5a098ecb3..e4a7cfd7bec 100644
> --- a/gcc/cp/expr.cc
> +++ b/gcc/cp/expr.cc
> @@ -214,24 +214,7 @@ mark_use (tree expr, bool rvalue_p, bool read_p,
>        gcc_fallthrough ();
>      CASE_CONVERT:
>        if (VOID_TYPE_P (TREE_TYPE (expr)))
> -	switch (TREE_CODE (TREE_OPERAND (expr, 0)))
> -	  {
> -	  case PREINCREMENT_EXPR:
> -	  case PREDECREMENT_EXPR:
> -	  case POSTINCREMENT_EXPR:
> -	  case POSTDECREMENT_EXPR:
> -	    tree op0;
> -	    op0 = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
> -	    STRIP_ANY_LOCATION_WRAPPER (op0);
> -	    if ((VAR_P (op0) || TREE_CODE (op0) == PARM_DECL)
> -		&& !DECL_READ_P (op0)
> -		&& (VAR_P (op0) ? warn_unused_but_set_variable
> -				: warn_unused_but_set_parameter) > 1)
> -	      read_p = false;
> -	    break;
> -	  default:
> -	    break;
> -	  }
> +	read_p = false;
>        recurse_op[0] = true;
>        break;
>  
> @@ -382,16 +365,7 @@ mark_exp_read (tree exp)
>        break;
>      CASE_CONVERT:
>        if (VOID_TYPE_P (TREE_TYPE (exp)))
> -	switch (TREE_CODE (TREE_OPERAND (exp, 0)))
> -	  {
> -	  case PREINCREMENT_EXPR:
> -	  case PREDECREMENT_EXPR:
> -	  case POSTINCREMENT_EXPR:
> -	  case POSTDECREMENT_EXPR:
> -	    return;
> -	  default:
> -	    break;
> -	  }
> +	return;
>        /* FALLTHRU */
>      case ARRAY_REF:
>      case COMPONENT_REF:


	Jakub



More information about the Gcc-patches mailing list