c++ - How to raise warning if return value is disregarded? -
i'd see places in code (c++) disregard return value of function. how can - 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 = 7; f(i); ///// <<----- here disregard return value return 1; }
please note that:
- it should work if function , use in different files
- free static check tool
you want gcc's warn_unused_result
attribute:
#define warn_unused __attribute__((warn_unused_result)) int warn_unused f(int z) { return z + (z*2) + z/3 + z*z + 23; } int main() { int = 7; f(i); ///// <<----- here disregard return value return 1; }
trying compile code produces:
$ gcc test.c test.c: in function `main': test.c:16: warning: ignoring return value of `f', declared attribute warn_unused_result
you can see in use in linux kernel; have __must_check
macro same thing; looks need gcc 3.4 or greater work. find macro used in kernel header files:
unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n);
Comments
Post a Comment