Reputation: 1252
Is it possible to tell GCC to use warn_unused_result flag for all function even not having the corresponding attribute? Because if I can forget to check for return value, I can also forget to add the GCC specific attribute.
I've seen it to be possible with some other compilers.
Upvotes: 12
Views: 2845
Reputation: 5152
When using objc, you could suppress warn_unused_result
warning like this,
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-result"
[YPKWhatsNewContainerViewModel checkWhatsNew]; // return value should be ignored
#pragma clang diagnostic pop
Upvotes: 0
Reputation: 16553
There is a clang plugin, in elfs-clang-plugins (authored by myself, open source) that helps.
While not for GCC, it can still be useful e.g. if you can add it to your CI gatekeeping process (if you have one) or just run it manually once in a while.
The warn_unused_result plugin will emit a warning for functions that lack a warn_unused_result attribute. The plugin only considers functions declared or defined in the current compilation unit, so external libraries don't add noise.
The plugin accepts an optional arg, --static-only which causes it to warn only about static functions (for when changing external APIs is too much work).
Example:
int foo(void);
Compiler output:
/tmp/test.c:1:5: warning: missing attribute warn_unused_result
int foo(void);
Upvotes: 0
Reputation: 44
While it does not seem possible with GCC, you can run static analyzers like coverity and lint to catch these.
Upvotes: 0
Reputation: 34628
No, you can only tell gcc to ignore all warn_unused_result
flags with -Wno-unused-result
but the respective -Wunused-result
only sets the default (to warn only on flags). Compiling with -Wall -Wextra -pedantic
should have triggered a warning if it can be activated but it doesn't, so it cannot.
Besides that, I wonder why you would want this, it is not that uncommon to ignore the result of functions, and all libraries are likely to produce tons of warnings.
Upvotes: 4