Ignoring return values in C

2019-01-17 14:23发布

Lately, I started using lint for static code analysis. One of the warning I get sometimes is regarding this issue. Let's say for instance that I've got the following function:

uint32_t foo( void );

And let's say that I delibertly ignore the return value of the function. To make the warning dissapear, one can write

(void) foo();

My question is, what is the "proper" way to write code like this, should I continue as I always did, since the compiler doesn't complain about it, or should I use the void for clarity, so other code maintainer will know that I delibertly ignored the return value.

When I look at the code like this ( with the void ), it looks pretty strange to me...

10条回答
Fickle 薄情
2楼-- · 2019-01-17 14:35

One way to do this with Clang and GCC compilers is with a pragma:

    /* ... */

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result" 

    foo(); /* this specific unused-result warning gets ignored during compilation */

#pragma GCC diagnostic pop 

    /* ... */

The push-pop combination wraps the ignored directive so that warnings can be triggered elsewhere in your code. It should be easier for anyone reading your source code down the road to see what this code block does.

查看更多
一夜七次
3楼-- · 2019-01-17 14:39

I personally like the "unused" warnings, but on occasion there are instances where I have to ignore them (e.g., the write() to user, or fscanf(...,"%*s\n") or strtol() where the return value is unimportant and I just want the side effect of [maybe] moving the file pointer along.)

With gcc 4.6, it's getting quite tricky.

  • Casting to (void) no longer works.
  • Re-writing functions (especially variadic) is tedious and clumsy.
  • {ssize_t ignore; ignore=write(...);} throws up another warning (assigned-not-used).
  • write(...)+1 throws up yet another warning (computed-value-not-used).

The only good (if ugly) way to suppress these is to convert the return value into something that the compiler agrees that you can ignore.

E.g., (void)(write(...)+1).

This is apparently progress. (And +0 does not work, BTW.)

查看更多
神经病院院长
4楼-- · 2019-01-17 14:40

The common way is to just call foo(); without casting into (void).

He who has never ignored printf()'s return value, cast the first stone.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-17 14:43

It is entirely legal and acceptable to write code that ignores the return value in some cases. The program below has very little reason to check the return value of printf().

int main(void) {
  printf("Hello world\n");
  return 0;
}
查看更多
Luminary・发光体
6楼-- · 2019-01-17 14:44

I like to compile my codes with the flags:

$gcc prog1.c -o prog1.x -Wall -Wextra -ansi -pedantic-errors -g -O0 -DDEBUG=1

And to avoid -Wunused-result I don't like the idea of adding another flag: -Wno-unused-result (if you do, thats one solution).

I used to cast to (void) for some functions (not printf or other famous, as the compilers dont warn about them, just the strange ones). Now casting to (void) does not work anymore (GCC 4.7.2)

Funny splint advises:

Result returned by function call is not used. If this is intended,
can cast result to (void) to eliminate message. (Use -retvalother to
inhibit warning)

But this is not a solution anymore. Splint needs an update regarding this issue.

So, to get rid of the warning in a very compatible way, here is a good MACRO:

/** Turn off -Wunused-result for a specific function call */
#define igr(M) if(1==((int)M)){;}

And call it like this:

igr(PL_get_chars(t, &s, CVT_VARIABLE));

Its a clean look, and any compiler will eliminate the code. Bellow a picture of my preferred editor vi: left window, no igr(); middle window, using igr(); right window, source.

enter image description here

You can see, its exactly the same, a completely innocuous code that let C do what gcc won't let: ignore the return code.

The comparison 1==... is necessary only to avoid splint warning that this conditional is no BOOL. GCC couldn't care less. Depending on the function, you might get a cast warning. I did a test ignoring a double with this MACRO and it was good, but somehow I'm not fully convinced. Specially if the function returns a pointer or something more complex.

In this case you will also need:

#define pigr(M) if(NULL==((void *)M)){;}

Last thing: the {;} is necessary because of the -Wempty-body warning (suggest braces around empty body in an ‘if’ statement).

And (now the last thing) the ; after the function call is not (strictly) necessary, but its good practice. Makes your code lines more homogeneous, all of them ending in a ;. (It's translated as a NOP mnemonic, and after optimization, disappear).


Running the compiler gives no warning or errors. Runing splint gives:

$ splint ./teste.c -I/usr/lib/swi-prolog/include/ -strict-lib
Splint 3.1.2 --- 20 Feb 2009

Finished checking --- no warnings

See also this answer

查看更多
聊天终结者
7楼-- · 2019-01-17 14:46

gnulib has this: http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/ignore-value.h

/* Normally casting an expression to void discards its value, but GCC
   versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
   which may cause unwanted diagnostics in that case.  Use __typeof__
   and __extension__ to work around the problem, if the workaround is
   known to be needed.  */
#if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
# define ignore_value(x) \
    (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
#else
# define ignore_value(x) ((void) (x))
#endif
查看更多
登录 后发表回答