This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Request for suppressing "warn_unused_result" warnings
- From: Paolo Bonzini <bonzini at gnu dot org>
- To: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr at ncbi dot nlm dot nih dot gov>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, "Vakatov, Denis (NIH/NLM/NCBI) [E]" <vakatov at ncbi dot nlm dot nih dot gov>
- Date: 2010年5月28日 15:12:20 +0200
- Subject: Re: Request for suppressing "warn_unused_result" warnings
- References: <A9D8BF3D8A74DF4A925FB541C0F39D2A01FD02135D@NIHMLBX15.nih.gov>
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
style.
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.
Paolo