This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Disabling warn_unused_result warnings on a case-by-case basis
- From: Manuel LÃpez-IbÃÃez <lopezibanez at gmail dot com>
- To: Florian Weimer <fweimer at redhat dot com>, "gcc at gnu dot org" <gcc at gnu dot org>
- Date: Tue, 14 Jun 2016 17:01:50 +0100
- Subject: Re: Disabling warn_unused_result warnings on a case-by-case basis
- Authentication-results: sourceware.org; auth=none
- References: <36a642d5-250b-3563-9216-04889252ad40 at redhat dot com>
On 14/06/16 10:32, Florian Weimer wrote:
A long time ago, GCC decided that warn_unused_result warnings should *not* be
silenced by casting to void, as in:
(void) write (STDOUT_FILENO, message, strlen (message));
Apparently, programmers have figured out to use this idiom as a replacement:
if (write (STDOUT_FILENO, message, strlen (message))) { }
I'm not sure if this is an improvement. The (void) idiom seems to make the
programmer intention more explicit.
Maybe it's time to reconsider and suppress the warning for casts to (void), too?
There is a thorough discussion and some analysis here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
I think most of the pushback against the (void) idiom has actually come from
GNU libc in the past.
One possible solution, if both behaviours are desired, is to warn for the void
idiom only for a new -Wstrict-unused-result, such that:
write (STDOUT_FILENO, message, strlen (message));
// warning: ignoring return value of âwriteâ, declared with attribute
warn_unused_result [-Wunused-result]
(void) write (STDOUT_FILENO, message, strlen (message));
// warning: ignoring return value of âfooâ, declared with attribute
warn_unused_result [-Wstrict-unused-result]
and -Wno-strict-unused-result disables warnings for (void).
But perhaps it is simpler to just change the default. It seems unlikely to use
(void) unintentionally.
Cheers,
Manuel.