I like to compile my codes with the flags:
$gcc prog1.c -o prog1.x -Wall -Wextra -ansi -pedantic-errors -g -O0 -DDEBUG=1
And to avoid -Wunused-result
I don't like the idea of adding another flag: -Wno-unused-result
(if you do, thats one solution).
I used to cast to (void)
for some functions (not printf
or other famous, as the compilers dont warn about them, just the strange ones). Now casting to (void)
does not work anymore (GCC 4.7.2)
Funny splint advises:
Result returned by function call is not used. If this is intended,
can cast result to (void) to eliminate message. (Use -retvalother to
inhibit warning)
But this is not a solution anymore. Splint needs an update regarding this issue.
So, to get rid of the warning in a very compatible way, here is a good MACRO
:
/** Turn off -Wunused-result for a specific function call */
#define igr(M) if(1==((long)M)){;}
And call it like this:
igr(PL_get_chars(t, &s, CVT_VARIABLE));
Its a clean look, and any compiler will eliminate the code. Bellow a picture of my preferred editor vi
: left window, no igr()
; middle window, using igr()
; right window, source.

You can see, its exactly the same, a completely innocuous code that let C do what gcc won't let: ignore the return code.
The comparison 1==...
is necessary only to avoid splint warning that this conditional is no BOOL
. GCC couldn't care less. Depending on the function, you might get a cast
warning. I did a test ignoring a double
with this MACRO and it was good, but somehow I'm not fully convinced. Specially if the function returns a pointer or something more complex.
In this case you will also need:
#define pigr(M) if(NULL==((void *)M)){;}
Last thing: the {;}
is necessary because of the -Wempty-body
warning (suggest braces around empty body in an ‘if’ statement).
And (now the last thing) the ;
after the function call is not (strictly) necessary, but its good practice. Makes your code lines more homogeneous, all of them ending in a ;
. (It's translated as a NOP
mnemonic, and after optimization, disappear).
Running the compiler gives no warning or errors. Runing splint
gives:
$ splint ./teste.c -I/usr/lib/swi-prolog/include/ -strict-lib
Splint 3.1.2 --- 20 Feb 2009
Finished checking --- no warnings
See also this answer