Make warn_unused_result applicable to the whole function using GCC

Is it possible to tell GCC to use the warn_unused_result flag for all functions that do not even have the corresponding attribute? Because if I can forget to check the return value, I can also forget to add a special GCC attribute.

I have seen how this is possible with some other compilers.

+8
c gcc compiler-construction compiler-warnings
source share
3 answers

No, you can only gcc ignore all warn_unused_result flags with -Wno-unused-result , but the corresponding -Wunused-result sets only by default (for warning only by flags). Compiling with -Wall -Wextra -pedantic should have -Wall -Wextra -pedantic warning if it can be activated, but it is not, therefore it cannot.

In addition, I wonder why you need it, it is not uncommon to ignore the result of functions, and all libraries probably generate a lot of warnings.

+1
source share

While GCC is not possible, you can run static analyzers such as coverty and lint to catch them.

0
source share

There is a clang plugin in elfs-clang-plugins (author myself, open source) that helps.

Not yet for GCC, it might be useful, for example. if you can add it to your CI control process (if you have one) or just run it manually once in a while.

The warn_unused_result plugin will issue a warning for functions that do not have the warn_unused_result attribute. The plugin only considers functions declared or defined in the current compilation unit, therefore external libraries do not add noise.

The plugin accepts the optional arg argument, -static-only, which forces it to warn only about static functions (since 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); 
0
source share

All Articles