Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

gcc

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.

like image 355
Frank Avatar asked Apr 13 '11 20:04

Frank


People also ask

How do I enable warnings in GCC?

This warning is enabled by -Wall or -Wextra . Warn when a #pragma directive is encountered that is not understood by GCC. If this command-line option is used, warnings are even issued for unknown pragmas in system header files. This is not the case if the warnings are only enabled by the -Wall command-line option.

Which option can be used to display compiler warnings?

Use WarningLevel to specify the level of warnings that you want the compiler to display. Use NoWarn to disable certain warnings.

What are compiler warnings?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.


1 Answers

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!

like image 168
Claudiu Avatar answered Nov 14 '22 23:11

Claudiu