Skip to content

silence unused variable warning#265

Open
michaelforney wants to merge 1 commit intoonetrueawk:masterfrom
michaelforney:unused
Open

silence unused variable warning#265
michaelforney wants to merge 1 commit intoonetrueawk:masterfrom
michaelforney:unused

Conversation

@michaelforney
Copy link
Copy Markdown
Contributor

gcc 16 got smarter and now complains about the (intentionally) unused variable:

run.c:1981:13: warning: variable 'unused' set but not used [-Wunused-but-set-variable=]

This variable appears to have been added solely to silence an unused return value warning. I'm not sure what combination of warning flags, compiler, and OS on which that was the case, but I believe a cast to void is the intended way to silence such warnings.

Trying to trick the compiler about a variable being used seems like a losing battle.

gcc 16 got smarter and now complains about the (intentionally)
unused variable:

run.c:1981:13: warning: variable 'unused' set but not used [-Wunused-but-set-variable=]

This variable appears to have been added solely to silence an unused
return value warning. I'm not sure what combination of warning
flags, compiler, and OS on which that was the case, but I believe
a cast to void is the intended way to silence such warnings.

Trying to trick the compiler about a variable being used seems like
a losing battle.
@arnoldrobbins
Copy link
Copy Markdown
Collaborator

I am pretty sure that was my code. With some earlier version of gcc casting to void didn't work, as I said in the comments. Compilers are getting "too" smart. Bleah. Thanks for the patch.

@michaelforney
Copy link
Copy Markdown
Contributor Author

I am pretty sure that was my code. With some earlier version of gcc casting to void didn't work, as I said in the comments. Compilers are getting "too" smart. Bleah. Thanks for the patch.

I am a bit curious about where you encountered that. Do you know which operating system you encountered that error on? I tried a few gcc versions, but I didn't see the warning even with -Wunused-result. Perhaps your system had a warn_unused_result attribute on wctomb? It looks like if that's the case the warning will show even with the void cast.

@arnoldrobbins
Copy link
Copy Markdown
Collaborator

I am pretty sure that was my code. With some earlier version of gcc casting to void didn't work, as I said in the comments. Compilers are getting "too" smart. Bleah. Thanks for the patch.

I am a bit curious about where you encountered that. Do you know which operating system you encountered that error on? I tried a few gcc versions, but I didn't see the warning even with -Wunused-result. Perhaps your system had a warn_unused_result attribute on wctomb? It looks like if that's the case the warning will show even with the void cast.

Indeed, that is the case on my system. Interestingly, using a cast to void with gcc 16 there is no warning, but with the stock gcc (13.x) it does warn. Once again: bleah. :-( (I'm on Ubuntu 24.04)

@michaelforney
Copy link
Copy Markdown
Contributor Author

Indeed, that is the case on my system. Interestingly, using a cast to void with gcc 16 there is no warning, but with the stock gcc (13.x) it does warn. Once again: bleah. :-( (I'm on Ubuntu 24.04)

Ah, I see. I found a lengthy and heated discussion about this here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425

I can't explain why you're seeing the warning go away with gcc 16, though.

Another option mentioned in the comments there is

if (wctomb(NULL, L'\0')) { /* silence unused result warning */ }

but my preference is just the void cast, especially if it works with gcc 16.

@arnoldrobbins
Copy link
Copy Markdown
Collaborator

@michaelforney I sorta skimmed that discussion. Amazing that it went on for NINE YEARS! Sheesh.

FWIW, the other option to deal with GCC is use #pragma and push and pop disabling of the particular warning. It's ugly, but it's no-nonsense and it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants