The handling of warn_unused_result is rather inconsistent, it warns in cases
where it shouldn't and sometimes doesn't warn in cases where it should. Try
the following to see this:
-- Snip --
> cat > test.c
#include <stdlib.h>
__attribute__(( warn_unused_result )) int foo( void )
{
return 1;
}
int main( int argc, char *argv[] )
{
int result;
result = foo();
result = foo();
if( !result )
exit( EXIT_FAILURE );
( void ) foo(); // Line 18
return EXIT_SUCCESS;
}
^D
>gcc -Wall test.c
test.c: In function 'main':
test.c:18: warning: ignoring return value of 'foo', declared with attribute
warn_unused_result
-- Snip --
The result from the first call to foo() is never used, but gcc doesn't warn
about it. On the other hand the result from the third call to foo() (line 18)
is explicitly discarded but this time gcc does warn about it. Just to provide
more detail on the gcc version:
-- Snip --
>gcc --ver
Using built-in specs.
Target: powerpc-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --disable-softfloat --enable-secureplt
--enable-targets=powerpc-linux,powerpc64-linux --with-cpu=default32
--with-long-double-128 --enable-checking=release powerpc-linux-gnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
-- Snip --
I've seen the same behaviour on one of the 4.2 versions as well so this doesn't
seem to be specific to 4.1.2, that's just the one that happens to be on the
machine I'm currently using.
--
Summary: __attribute__(( warn_unused_result )) warns when it
shouldn't, doesn't warn when it should
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: pgut001 at cs dot auckland dot ac dot nz
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35579