Frank
Frank

Reputation: 66224

How to turn on gcc warnings for a forgotten return statement?

How do I turn on gcc warnings for a forgotten return statement?

It is supposed to warn me in cases like the following:

int foo() {
  std::cout << "haha";
}

I know -Wall turns that warning on, but it enables too many other warnings.

Upvotes: 6

Views: 1483

Answers (2)

Batman
Batman

Reputation: 1324

always use:

gcc -g -ansi -pedantic -Wall -o

Upvotes: -1

Claudiu
Claudiu

Reputation: 229561

According to gcc's online documentation, -Wall turns on:

      -Waddress   
      -Warray-bounds (only with -O2)  
      -Wc++0x-compat  
      -Wchar-subscripts  
      -Wenum-compare (in C/Objc; this is on by default in C++) 
      -Wimplicit-int (C and Objective-C only) 
      -Wimplicit-function-declaration (C and Objective-C only) 
      -Wcomment  
      -Wformat   
      -Wmain (only for C/ObjC and unless -ffreestanding)  
      -Wmissing-braces  
      -Wnonnull  
      -Wparentheses  
      -Wpointer-sign  
      -Wreorder   
      -Wreturn-type  
      -Wsequence-point  
      -Wsign-compare (only in C++)  
      -Wstrict-aliasing  
      -Wstrict-overflow=1  
      -Wswitch  
      -Wtrigraphs  
      -Wuninitialized  
      -Wunknown-pragmas  
      -Wunused-function  
      -Wunused-label     
      -Wunused-value     
      -Wunused-variable  
      -Wvolatile-register-var 

Out of those, -Wreturn-type seems like it would do the trick:

Warn whenever a function is defined with a return-type that defaults to int. Also warn about any return statement with no return-value in a function whose return-type is not void (falling off the end of the function body is considered returning without a value), and about a return statement with an expression in a function whose return-type is void.

However, if turning on -Wall makes your code have way too many warnings, I'd recommend fixing up your code!

Upvotes: 18

Related Questions