Request for suppressing "warn_unused_result" warnings

Paolo Bonzini
Fri May 28 15:36:00 GMT 2010

On 05/28/2010 06:36 AM, Lavrentiev, Anton (NIH/NLM/NCBI) [C] wrote:
> Dear GCC developers,
> Would you please consider suppressing (relatively new) warnings like
> this one:
> ignoring return value of 'int chdir(const char*)', declared with
> attribute warn_unused_result
> in cases when the source code explicitly casts the result to (void).
> Like in
> (void) chdir("/");
> There is no check necessary in this rare case, yet the compiler
> seems to ignore the explicit cast (which tells the developer's
> intention exactly: there is nothing to check), and keeps issuing the
> warning, regardless.
> Making dummy assignment and/or check around such a chdir call (just
> to satisfy the "unused result" requirement) will not help make the
> code any cleaner.
> There are always a subtle number of cases for almost any "__wur"
> call that do not need any result consumption.  Yet the warning in
> general is very helpful in catching the situation where the result is
> not consumed *explicitly*.
> We're lagging behind the GCC development here and perhaps the newest
> version of the compiler does already implement this suggestion -- so
> let me apologize for the noise then.
> Thanks for considering this suggestion,

Bugzilla unfortunately has a lot of discussion about this issue, and the
outcome was that the feature was done this way by design.

What you reported is not the only common intended violation of __wur.
It is for example possible to ignore the result of fwrite and defer
error checking to fflush or fclose, but __wur does not help with this

I suggest that you add two functions like these:

static inline void ignore_value (int i) { (void) i; }
static inline void ignore_ptr (void* p) { (void) p; }

that you can use instead of the (void) cast.

Nevertheless, thanks for writing your report in a very polite way, which
didn't occur in some of the previous cases.


More information about the Gcc mailing list