Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ How to get warning on ignoring function return value

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.

like image 934
Arun Avatar asked May 20 '10 01:05

Arun


People also ask

How do you ignore return value?

To ignore a return value, assign it to _ ; this is like an implicitly declared variable that cannot be referenced. When a return type includes an error, you have to do something with the error. _ is of type any: you cannot use _ to ignore an error.

What happens when we don't get the returned value from a function?

The returned value simply gets ignored. If a function returns a class instance, the class instance gets destroyed, which results in an invocation of the class's defined destructor, or a default destructor.

What does return value ignored mean in C?

It's a warning that stops your compiler from performing it's task (too strict settings). Check the return value of the scanf() function for errors and the warning should disappear.

What does return value ignored scanf mean?

scanf() returns a value, and your original code was ignoring (not using) it. Your compiler has an option enabled asking for a warning whenever a result is ignored, so you're getting that warning (not an error, unless you also have something like -Werror)


1 Answers

Thanks to WhirlWind and paxdiablo for the answer and comment. Here is my attempt to put the pieces together into a complete (?) answer.

-Wunused-result is the relevant gcc option. And it is turned on by default. Quoting from gcc warning options page:

-Wno-unused-result

Do not warn if a caller of a function marked with attribute warn_unused_result (see Variable Attributes) does not use its return value. The default is -Wunused-result

So, the solution is to apply the warn_unused_result attribute on the function.

Here is a full example. The contents of the file unused_result.c

int foo() { return 3; }  int bar() __attribute__((warn_unused_result)); int bar() { return 5; }  int main() {     foo();     bar();    /* line 9 */     return 0; } 

and corresponding compilation result:

$gcc unused_result.c  unused_result.c: In function ‘main’: unused_result.c:9: warning: ignoring return value of ‘bar’, declared with attribute warn_unused_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-result in 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 with warn_unused_result.

like image 162
Arun Avatar answered Sep 19 '22 14:09

Arun