lint produces some warning like:
foo.c XXX Warning 534: Ignoring return value of function bar()
From the lint manual
534 Ignoring return value of function
‘Symbol’ (compare with Location) A
function that returns a value is
called just for side effects as, for
example, in a statement by itself or
the left-hand side of a comma
operator. Try: (void) function(); to
call a function and ignore its return
value. See also the fvr, fvo and fdr
flags in §5.5 “Flag Options”.
I want to get this warning, if there exists any, during compilation. Is there any option in gcc/g++ to achieve this? I had turned on -Wall but that apparently did not detect this.
Thanks to WhirlWind and paxdiablo for the answer and comment. Here is my attempt to put the pieces together into a complete (?) answer.
-Wunused-resultis the relevant gcc option. And it is turned on by default. Quoting from gcc warning options page:So, the solution is to apply the
warn_unused_resultattribute on the function.Here is a full example. The contents of the file unused_result.c
and corresponding compilation result:
Note again that it is not necessary to have -Wunused-result since it is default. One may be tempted to explicitly mention it to communicate the intent. Though that is a noble intent, but after analyzing the situation, my choice, however, would be against that. Because, having
-Wunused-resultin the compile options may generate a false sense of security/satisfaction which is not true unless the all the functions in the code base are qualified withwarn_unused_result.