#625357 gcc -Wunused-but-set-variable warning is confusing

Package:
gcc-snapshot
Source:
gcc-snapshot
Description:
SNAPSHOT of the GNU Compiler Collection
Submitter:
Matthias Klose
Date:
2021-05-08 13:00:02 UTC
Severity:
minor
Tags:
#625357#5
Date:
2011-05-03 10:33:20 UTC
From:
To:
This package builds with -Werror, and GCC 4.6 triggers new warnings
which will make the package fail to build.  Currently a Debian patch
just passes
    -Wno-error=unused-but-set-variable and
    -Wno-error=unused-but-set-parameter
to avoid build failures, but this patch will be reverted with the
GCC 4.6.1 release, and the severity of the report will be raised.

The full build log can be found at:
http://people.debian.org/~doko/tmp/werror/jigit_1.18-1_lsid64.buildlog
The last lines of the build log are at the end of this report.

#625357#10
Date:
2011-05-29 13:51:40 UTC
From:
To:
reassign 625357 gcc-4.6
thanks

I'll remove the -Werror to stop gcc breaking the build here, but I
definitely believe that gcc is doing the wrong thing here.
Technically, yes - the variables are set but unused. However, this is
a far higher level of pedantry than is warranted for -Wall. The code
being complained about is perfectly valid, typical of the defensive
programming pattern of "initialise all variables". I hence reject this
as a bug.

#625357#19
Date:
2011-06-01 22:05:27 UTC
From:
To:
Hi Steve,

Steve McIntyre wrote:

The -Wall option has implied -Wunused-variable for a very long time;
the idea as I understand it was to detect dead code which might be an
indicator for other problems (typos or planned functionality that was
left out) and which is distracting.  As I understand it, the only
reason -Wunused-but-set-variable did not exist until recently was a
small accident of implementation, that technically "x = 5;" is an
expression that evaluates "x".

I believe the best way to avoid breaking the build is to simply not use
-Werror on autobuilders.  I would like to teach autobuilders to send
warnings by email to interested maintainers so maintainers could get the
benefit of notification without making the release team and toolchain
maintainers' lives more difficult; would you be interested in that?

Thanks and hope that helps,
Jonathan

#625357#24
Date:
2011-06-02 18:16:59 UTC
From:
To:
severity 625357 normal
retitle 625357 gcc -Wunused-but-set-variable should not be implied by -Wall (?)
tags 625357 = upstream moreinfo
quit

Hi again,

Steve McIntyre wrote:

 $ program_one='int main(void) { int x = 5; return 0; }'
 $ program_two='int main(void) { int x; x = 5; return 0; }'
 $ echo "$program_one" | gcc-4.4 -Wall -x c -
 <stdin>: In function ‘main’:
 <stdin>:1: warning: unused variable ‘x’
 $ echo "$program_two" | gcc-4.4 -Wall -x c -

So imho gcc before v4.6 is just fundamentally confused.  Which means
there are a number of ways one could go with this.  Do you mean:

 A. Unused variables are not a big deal, and they belong in -Wextra,
    not -Wall.

 B. New warnings are a pain in the neck and should go in -Wextra
    during a transition period.

 C. The idiom

	ssize_t unused;
	/*
	 * Yes, there might be an error, dear gcc -D_FORTIFY_SOURCE,
	 * but we want to ignore it.
	 */
	unused = write(...);

   in place of, say,

	/* loop on partial writes and EINTR */
	xwrite(...);

   is good style and the -Wunused-but-set-variable warning is
   fundamentally misguided.

 D. The idiom

	int x;
	int y;

	... ordinary code ...
	#if SOMETIMES
	... code to set and use x ...
	#endif

    in place of

	#if SOMETIMES
	int x;
	#endif
	int y;

	... ordinary code ...
	#if SOMETIMES
	... code to set and use x ...
	#endif

    or

	int y;

	... ordinary code ...
	#if SOMETIMES
	{
		int x;
		... code to set and use x ...
	}
	#endif

    is good style and the longstanding -Wunused-variable warning is
    fundamentally misguided.

 D. Something else?

Sorry for the lack of clarity before.
Jonathan

#625357#35
Date:
2011-06-03 14:55:34 UTC
From:
To:
The -Werror flag itself isn't the real problem for me, and I've
already removed it in my upload. I'm much more worried about (IMHO)
broken decisions being made in gcc about the levels of some
warnings. See my second mail in a few minutes...

Potentially, yes.

#625357#40
Date:
2011-06-03 15:30:43 UTC
From:
To:
Sure, that's confused. :-)

No.

Maybe, but a lot of people will never see them anyway until they cause
build failures. :-)
various things to respond to you here. It looks like I've missed
*exactly* the thing gcc was complaining about, and it is case C
here. Now I can see that, I'm less unhappy about the warning.

My code is analogous to your example code:

static int parse_md5_entry(char *md5_entry)
{
    int error = 0;
    char *file_name = NULL;
    char *md5 = NULL;
    unsigned char bin_md5[16];
    int i;

    ...
    /* code that does the real meat of the work to fill in "md5" and
       "filename" , but doesn't set/use "error" at all */
    ...

    error = add_md5_entry(UNKNOWN, md5, file_name);
    return 0;
}

To explain a bit more: the warning reported by gcc (unhelpfully)
points at the declaration/initialisation of "error", which led me to
(incorrectly) think that it was complaining about set-but-unused for
the initialisation. With minor tweaks, I can see that it's complaining
about the return value from add_md5_entry() being ignored but doesn't
*say* that. :-(

I'm about to update the code around that area now. More accurate
diagnostics from gcc would be a major improvement, I think!

#625357#45
Date:
2011-06-04 07:37:21 UTC
From:
To:
tags 625357 - moreinfo
retitle 62537 gcc -Wunused-but-set-variable warning is confusing
# bad error message
severity 625357 minor
quit

Steve McIntyre wrote:

Ah, I see and I think I agree.  The current message

 test.c:3:6: warning: variable ‘x’ set but not used [-Wunused-but-set-variable]

places the emphasis on the assignment (which is not worth warning
about), when the warning is instead supposed to be about the variable
(which is never used).  Something like

 test.c:3:6: warning: unused variable ‘x’ [-Wunused-but-set-variable]

would be much clearer.

Will think more and then work on a patch.  Thanks for your patient
explanations.

#625357#60
Date:
2012-01-05 23:35:23 UTC
From:
To:
reassign 625357 src:gcc-snapshot 20111210-1
affects 625357 + gcc-4.6
affects 625357 + gcc-4.7
quit

Jonathan Nieder wrote:
[...]

Here's a rough patch.  Will send upstream after testing, but thoughts
welcome already.  Sensible?

#625357#79
Date:
2021-05-08 12:45:54 UTC
From:
To:
Jonathan, what's the upstream status for this issue?