Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: warning on unused return [duplicate]

Tags:

c

gcc

Problem statement

I use a return integer to propagate errors through my code. I would like gcc to provide me with a warning if I accidentally forget to propagate the error (i.e. when I forget to use the return integer).

Example

Consider this very simple example test.c:

// header
int test ( int a );

// function
int test ( int a )
{

  if ( a<0 ) return 1;

  return 0;
}

// main program
int main ( void )
{

  int a = -10;

  test(a);

  return 0;
} 

Expected behavior

$ gcc -Wunused-result main.c
Warning: main.c:19 ... unused return ...

or alternatively using Wall or Wextra or another option.

Needless to say, my compiler does not provide the warning with any of these options. One of my mistakes already took me frustratingly long to find...

like image 977
Tom de Geus Avatar asked Oct 15 '25 11:10

Tom de Geus


1 Answers

// header
int test ( int a )  __attribute__ ((warn_unused_result));

// function
int test ( int a )
{

  if ( a<0 ) return 1;

  return 0;
}

// main program
int main ( void )
{

  int a = -10;

  test(a);

  return 0;
}

It should work now.

like image 150
anothertest Avatar answered Oct 17 '25 03:10

anothertest