6

As the title suggest, for example, in 85% of the situation, I'd like to check the return code of foo(), but sometimes I really don't care about it, but this will raise Coverity warning.

What's the best way to deal with this problem? Changing Coverity settings doesn't count. :)

solotim
  • 1,826
  • 4
  • 24
  • 41

1 Answers1

8

The correct way to suppress CHECKED_RETURN defect is to cast the return value you don't care about to a void. This has the additional advantage of making it clear to anyone reading the code that you don't care about return value, rather than that you forgot to check it.

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • in addition, trying to trick analysis with macros will not work since Coverity is not analyzing the source code but rather the AST. – Asya Kamsky Nov 12 '11 at 21:13
  • Coverity still flagged `(void)fread(..)` with this error for me after adding the cast to void. Perhaps the option doesn't work for syscalls/functions flagged 'always check'. It was legitimate though, a separate ferror() was covering the failure cases. – davenpcj Jun 22 '15 at 21:02
  • right, statistical checkers honor (void) but ones that are explicitly asked to be checked will flag failure to check even with attempt to suppress. There is always comment annotations to suppress *those* when needed :) – Asya Kamsky Jun 27 '15 at 22:13