是否有任何编译器或选项可以针对无意义和有错误的 switch 语句触发警告?
给出以下代码:
bool doGoodThing;
switch (some_variable)
{
case 1:
case 2:
doGoodThing = true;
default:
doGoodThing = false;
}
最新的 gcc 版本足够智能,可以检测变量何时被分配但仍未使用,等等。是否有任何编译器或标志可以触发警告,指出整个开关对于这样的代码没有任何意义?
更新:问题与doGoodThing
变量无关。这是一个愚蠢的 switch
语句,没有多大意义。
更新 2:在将其标记为重复项之前,先让“欺骗”选民通过 - 再读一遍问题。这不是关于“case”之后缺少“break”语句的警告。这是关于死代码、逻辑错误、编译器对代码的静态语义分析。如果不存在“break”,我不需要警告。
Given the following code:
bool doGoodThing;
switch (some_variable)
{
case 1:
case 2:
doGoodThing = true;
default:
doGoodThing = false;
}
Latest gcc
version are smart enough to detect when variables are being assigned to but still not used, etcetera. Is there any compiler or flag that could trigger a warning saying that the whole switch does not make any sense for a code like this?
UPDATE: The question is not about doGoodThing
variable. It is about a silly switch
statement that doesn't make much sense.
UPDATE 2: Passing "dupe" voters, before you mark it as a duplicate - read a question one more time. This is not about a warning for a missing "break" statement after "case". This is about dead code, logical errors, static semantics analysis of the code by compiler. I do not need a warning if "break" is not there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在谈论 switch 语句中缺少的中断,那么它是一个要求的增强在海湾合作委员会。
编辑2:如果您使用的是Java,FindBugs可以为您做到这一点
再次编辑:< a href="http://cppcheck.sourceforge.net/" rel="nofollow">CPPCheck似乎检测相关内容:
If you're talking about the missing break in the switch statement, it is an asked ehancement in GCC.
EDIT 2: if you were using Java, FindBugs could do it for you
EDIT again : CPPCheck seems to detect something related :
好的,所以我们正在寻找一个
可以产生警告的规则,但是
(这可能是这里的预期代码)不会产生警告。解决这个问题的一种相对简单的方法是始终警告失败,除非箱子是空的。即不警告从情况 1 到情况 2 的失败,因为中间没有代码,但警告从情况 2 到默认情况的失败。然而,这种方法仍然会警告可能是故意的代码。
更复杂的规则是这样的:每当对变量 x 进行赋值而导致不存在可能的执行路径(从而无法使用分配的值)时,就会生成警告。即,在涉及为
x
分配值v
的所有可能执行路径中,x
要么被重新分配给其他内容,要么在之前超出范围任何使用x
的代码都会被执行。这会警告您的代码,但不会警告固定示例。执行此分析绝对是可能的。然而,我不知道目前有任何编译器进行此类分析(尽管这真正意味着 gcc 不这样做)。
Okay, so we're looking for a rule by which
would produce a warning, but
(which presumably is the intended code here) would not. One relatively simple way to go about this would be to always warn about fall-through except if a case is empty. I.e. don't warn about the fall through from case 1 to case 2 because there's no code in between, but warn about the fall through from 2 to default. However this approach would still warn about code that might be intentional.
A more sophisticated rule would be this: Produce a warning whenever there is an assignment to a variable
x
such that there is no possible execution path, such that the assigned value would be used. I.e. in all possible execution paths that involvex
being assigned the valuev
,x
will either be reassigned to something else or go out of scope before any code that usesx
is executed. This would warn about your code, but not the fixed example.Performing this analysis is definitely possible. However I'm not aware of any compiler that currently does such analysis (though all that really means is that gcc doesn't).