为什么编译器没有警告我一个空的if语句? [英] Why didn't the compiler warn me about an empty if-statement?

查看:3313
本文介绍了为什么编译器没有警告我一个空的if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Keil uVision v4.74并启用了All Warnings选项。

I'm using Keil uVision v4.74 and have enabled the option "All Warnings".

我写了以下故意代码:

if(condition matched)
{
 //do something
}

当我重建项目时,我得到0个错误,0个警告。

When I rebuilt my project, I got 0 errors, 0 warnings.

然而,当我意外地写道:

if(condition matched);
{
 //do something
}

我也有0错误,0警告。

I also got 0 errors, 0 warnings.

我几乎无法找到一个小的; 跟随如果条件是问题的根源。

It was next to impossible for me to find out that a small ; following the if condition was the root of the problem.

为什么编译器不将它视为警告并通知我?

Why didn't the compiler treat it as a warning and inform me?

推荐答案

这不是错误,因为空语句有效语句;然而,因为它肯定是可疑的代码,它是编译器警告的完美候选者 - 事实上 gcc -Wall -Wextra 确实警告过:

It's not an error because an empty statement is a valid statement; however, since it's certainly suspicious code it's the perfect candidate for a compiler warning - and in fact gcc -Wall -Wextra does warn about this:

int foo(int x) {
  if(x); {
    return 42;
  }
  return 64;
}

 

/tmp/gcc-explorer-compiler116427-37-l1vpg4/example.cpp: In function 'int foo(int)':
2 : warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if(x); {
^

https://godbolt.org/g/RG1o7t

clang 和VC ++也是这样做的。

both clang and VC++ do it too.

gcc 6甚至更聪明(好吧,也许太多),甚至将缩进作为暗示出现问题:

gcc 6 is even smarter (well, maybe too much), and takes even the indentation as a hint that something is wrong:

/tmp/gcc-explorer-compiler116427-76-1sfy0y/example.cpp: In function 'int foo(int)':
2 : warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if(x); {
^
2 : warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(x); {
^~
2 : note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
if(x); {
^

所以,要么你没有足够的警告,要么你的编译器不够智能。

So, either you don't have the warnings cranked up enough, or your compiler isn't smart enough.

如果你没有办法切换到更有用的编译器,可以考虑使用静态分析工具;例如,在这种情况下 cppcheck 发现错误(当给出 - enable = all --inconclusive 标志时) :

If you don't have the possibility to switch to a more helpful compiler, consider using static analysis tools; for example, in this case cppcheck spots the error (when given the --enable=all --inconclusive flags):

[mitalia@mitalia ~/scratch]$ cppcheck --enable=all --inconclusive emptyif.c 
Checking emptyif.c...
[emptyif.c:2]: (warning, inconclusive) Suspicious use of ; at the end of 'if' statement.
[emptyif.c:1]: (style) The function 'foo' is never used.






附录 - 各种编译器的相关警告(免费)更新)



回顾一下,相关的警告选项是:


Addendum - relevant warnings for various compilers (feel free to update)

to recap, the relevant warning options are:


  • gcc -Wempty-body ;包含在 -Wextra ;

  • gcc > = 6.0,还 -Wmisleading -indentation 可以提供帮助;包含在 -Wall ;

  • clang -Wempty-body ;包含在 -Wextra 中;

  • Visual C ++ C4390 ,包含在 / W3

  • gcc -Wempty-body; included in -Wextra;
  • gcc>=6.0, also -Wmisleading-indentation can help; included in -Wall;
  • clang -Wempty-body; included in -Wextra too;
  • Visual C++ C4390, included in /W3

静态分析工具:


  • cppcheck - enable = warning --inconclusive ;包含在中 - enable = all --inconclusive

  • cppcheck --enable=warning --inconclusive; included in --enable=all --inconclusive

这篇关于为什么编译器没有警告我一个空的if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆