你想要GCC的warn_unused_result
属性:
#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 i = 7;
f(i); ///// <<----- here i disregard the return value
return 1;
}
try 编译此代码会产生:
$ gcc test.c
test.c: In function `main':
test.c:16: warning: ignoring return value of `f', declared with
attribute warn_unused_result
你可以在Linux kernel中看到这个正在使用;他们有一个做同样事情的__must_check
宏;看起来你需要GCC 3.4或更高版本才能工作.然后,您将发现内核头文件中使用了该宏:
unsigned long __must_check copy_to_user(void __user *to,
const void *from, unsigned long n);