5

I'm searching for a compiler flag for gcc and if possible for clang and the Microsoft compilers as well, that triggers a warning (error with -Werror) if a non-void function is called without using the return value like this:

int test() {
    return 4;
}

int main(void) {
    test(); //should trigger a warning
    int number = test(); //shouldn't trigger the warning
    return 0;
}

If there is no such compiler flag, maybe some way to tell the clang static analyzer to complain about it.

EDIT: To clarify my original question: I actually meant using the return value, not only assigning it.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
FSMaxB
  • 2,280
  • 3
  • 22
  • 41
  • 1
    Note - Use `int main (void)` instead of `void`. . – ameyCU Sep 04 '15 at 06:56
  • 2
    That would cause lots of unnecessary warnings. For instance, every call to `printf()` would generate a complaint -- they return the number of characters printed, but no one really cares. – Barmar Sep 04 '15 at 06:56
  • 1
    @Barmar, I could see where this might be handy for non-library functions though. When I add a return value to a previously void function, I have to change the rest of the signature just to hunt down all the places that use it. – Leeor Sep 04 '15 at 07:01
  • Or you could use `grep`, or the IDE's built-in search facilities, like "where used". – user3386109 Sep 04 '15 at 07:07
  • There's a list of all GCC warning options here: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html – Barmar Sep 04 '15 at 07:08
  • possible duplicate of [how to raise warning if return value is disregarded - gcc or static code check?](http://stackoverflow.com/questions/2042780/how-to-raise-warning-if-return-value-is-disregarded-gcc-or-static-code-check) – Andreas Fester Sep 24 '15 at 08:27
  • @Andreas This question is related, but slightly different. – FSMaxB Sep 28 '15 at 07:24

2 Answers2

12

I never used it myself (do you really need it?), you can try

  • defining the function with warn_unused_result attribute
  • enabling -Wunused-result flag with gcc.

This will tell you about any unused value from the function return.


In case, any doubts, SEE IT LIVE or SEE IT LIVE AGAIN Thanks to M.M for the link in the comment

Or:

#include <stdio.h>

extern int func1(void) __attribute__((warn_unused_result));
extern int func2(void);

int main(void)
{
    func1();
    int rc1 = func1();
    int rc2 = func1();
    func2();
    printf("%d\n", rc1);
    return 0;
}

Compilation (GCC 5.1.0 on Mac OS X 10.10.5):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -c warn.c
warn.c: In function ‘main’:
warn.c:10:9: error: unused variable ‘rc2’ [-Werror=unused-variable]
     int rc2 = func1();
         ^
warn.c:8:5: error: ignoring return value of ‘func1’, declared with attribute warn_unused_result [-Werror=unused-result]
     func1();
     ^
cc1: all warnings being treated as errors
$
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Some static code analyzers like splint can check for these kind of things:

$ splint check.c
Splint 3.1.2 --- 03 May 2009

check.c: (in function main)
check.c:6:5: Return value (type int) ignored: test()
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalint to inhibit warning)

Unlike @Sourav's answer, this does not require a specific __attribute__ annotation on the target function, but on the other hand possibly emits many warnings. Its usually possible to suppress the warnings for specific functions or function calls by using annotations (like /*@alt void@*/).

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123