如何为遗忘的退货声明打开gcc警告?

Fra*_*ank 6 c c++ gcc

如何为遗忘的退货声明打开gcc警告?

它应该在以下情况下警告我:

int foo() {
  std::cout << "haha";
}
Run Code Online (Sandbox Code Playgroud)

我知道-Wall会发出警告,但它会启用太多其他警告.

Cla*_*diu 18

根据gcc的在线文档,-Wall打开:

      -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 
Run Code Online (Sandbox Code Playgroud)

其中,-Wreturn-type似乎它可以做到这一点:

每当使用默认为int的返回类型定义函数时发出警告.还要警告任何return语句没有返回值,返回类型不是void的函数(从函数体的末尾掉下来被认为是没有值返回),以及一个函数中带有表达式的return语句return-type无效.

但是,如果打开-Wall使您的代码有太多警告,我建议修复您的代码!