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.
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.
Use WarningLevel to specify the level of warnings that you want the compiler to display. Use NoWarn to disable certain 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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With