https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425

Filipe Brandenburger <filbranden at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |filbranden at google dot com

--- Comment #2 from Filipe Brandenburger <filbranden at google dot com> ---
It turns out clang from LLVM seems to do the right thing here...

Using this as a test case:

  __attribute__((warn_unused_result))
  int foo() {
    return -1;
  }

  int should_not_warn() {
     return foo();
  }

  int should_warn() {
     foo();
     return 0;
  }

  int void_cast_should_not_warn() {
     (void) foo();
     return 0;
  }

With gcc 5.1:

  $ gcc -c test.c 
  test.c: In function ‘should_warn’:
  test.c:12:4: warning: ignoring return value of ‘foo’, declared with attribute
warn_unused_result [-Wunused-result]
      foo();
      ^
  test.c: In function ‘void_cast_should_not_warn’:
  test.c:17:4: warning: ignoring return value of ‘foo’, declared with attribute
warn_unused_result [-Wunused-result]
      (void) foo();
      ^

With clang 3.5:

  $ clang-3.5 -c test.c 
  test.c:12:4: warning: ignoring return value of function declared with
warn_unused_result attribute [-Wunused-result]
     foo();
     ^~~
  1 warning generated.

Other static analysis tools seem to also take a hint out of the (void) cast.

I mean, why make the programmer jump through hoops and store the return value
in an unused variable? Isn't the (void) cast more explicit than that anyways?

Right now the only choice I can see is to use -Wno-unused-result which
basically makes the feature useless when building using gcc...

Richard Blener proposed a patch here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509#c10

Would it make sense to adopt that patch?

Cheers,
Filipe

Reply via email to