简体   繁体   English

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

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

How do I turn on gcc warnings for a forgotten return statement? 如何为遗忘的退货声明打开gcc警告?

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. 我知道-Wall会发出警告,但它会发出太多其他警告。

According to gcc's online documentation , -Wall turns on: 根据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 

Out of those, -Wreturn-type seems like it would do the trick: 其中, -Wreturn-type似乎可以解决问题:

Warn whenever a function is defined with a return-type that defaults to int. 每当使用默认为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. 还要警告任何return语句没有返回值,返回类型不是void的函数(从函数体的末尾掉落被认为是没有值返回) ,以及一个函数中带有表达式的return语句return-type无效。

However, if turning on -Wall makes your code have way too many warnings, I'd recommend fixing up your code! 但是,如果启用-Wall会使您的代码有太多警告,我建议修复您的代码!

always use: 总是使用:

gcc -g -ansi -pedantic -Wall -o gcc -g -ans--pedantic -Wall -o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM