c++ - 如何为忘记的返回语句打开 gcc 警告?

标签 c++ c gcc

如何为忘记的返回语句打开 gcc 警告?

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

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

我知道 -Wall 会打开该警告,但它会启用太多其他警告。

最佳答案

根据 gcc 的 online documentation , -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 

其中,-Wreturn-type 似乎可以解决问题:

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.

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

关于c++ - 如何为忘记的返回语句打开 gcc 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655181/

相关文章:

c++ - 如何将 paintEvent 连接到插槽?

c++ - move 包含 vector<unique_ptr<T>> 的可分配类

linux - gcc 是否有任何选项可以在 ELF 二进制文件中添加版本信息?

php - 如何在ubuntu中使用php脚本编译c程序

c++ - CPP : avoiding macro expansion of a macro function parameter

python - 什么是 python 字典和 zip 的最佳 C++ 替代品?

C 库代码的正确命名约定

c - 32 位系统中的结构填充

c - 如何在一个以指针为参数的函数中访问另一个以指针为参数的函数中的值?

c - OpenMP 与 gcc 编译器优化