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 can use this handy template to do it at run-time.
Instead of returning an error code (e.g. HRESULT) you return a return_code<HRESULT>, which asserts if it goes out of scope without the value being read. It's not a static analysis tool, but it's useful none the less.
Any static analysis code (e.g. PC-Lint) should be able to tell you that. For PC-Lint, I know that this is the case.