I’d like to see all the places in my code (C++) which disregard return value of a function. How can I do it – with gcc or static code analysis tool?
Bad code example:
int f(int z) {
return z + (z*2) + z/3 + z*z + 23;
}
int main()
{
int i = 7;
f(i); ///// <<----- here I disregard the return value
return 1;
}
Please note that:
- it should work even if the function and its use are in different files
- free static check tool
You want GCC’s
warn_unused_resultattribute:Trying to compile this code produces:
You can see this in use in the Linux kernel; they have a
__must_checkmacro that does the same thing; looks like you need GCC 3.4 or greater for this to work. Then you will find that macro used in kernel header files: