How to enable gcc warnings for a forgotten return statement?

How to enable gcc warnings for a forgotten return statement?

It is supposed to warn me in the following cases:

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

I know that -Wall includes this warning, but it allows too many other warnings.

+6
source share
2 answers

According to gcc online documentation, -Wall includes:

  -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 

Of these, -Wreturn-type seems to do the trick:

Warn when a function is defined with a return type that defaults to int. In addition, it warns of any return statement without a return value in a function whose return type is not invalid (falling from the end of the function body is considered return without a value) , as well as a return statement with an expression in the function whose return type is invalid.

However, if you enable -Wall , your code will have too many warnings, I would recommend fixing your code!

+18
source

always use:

gcc -g -ansi -pedantic -wall -o

-1
source

Source: https://habr.com/ru/post/885858/


All Articles