无间断切换
我有一些 switch 语句,如下所示。请注意,没有休息。 Findbugs 仅报告第二个 case 语句的错误。 错误是:发现 Switch 语句,其中一个案例落入下一个案例。
switch(x) {
case 0:
// code
case 1:
// code
case 2:
// code
}
I have some switch statement as shown below. Notice there is no break.
Findbugs is reporting error on the second case statement only.
The error is : Switch statement found where one case falls through to the next case.
switch(x) {
case 0:
// code
case 1:
// code
case 2:
// code
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Findbugs 指出,如果第一个案例中有任何代码,那么从一个案例过渡到下一个案例通常不是一个好主意(尽管有时它可以起到很好的效果)。因此,当它看到第二个
case
并且没有break
时,它会报告错误。例如:
这是完全有效的 Java,但它可能没有达到作者的预期:如果
foo
是0
,全部三个函数doSomething
、doSomethingElse
和doSomeOtherThing
运行(按顺序)。如果foo
为1
,则仅运行doSomethingElse
和doSomeOtherThing
。如果foo
是任何其他值,则仅运行doSomeOtherThing
。相反:
这里,只有一个函数会运行,具体取决于
foo
的值。由于忘记
break
是一种常见的编码错误,Findbugs 等工具会为您标记它。有一个常见的用例,您连续有多个
case
语句,并且没有中间代码:在那里,我们想要调用
doSomething
if <代码>foo是0
或1
。大多数工具不会将此标记为可能的编码错误,因为在case 1
之前的case 0
中没有代码,而且这是一种相当常见的模式。Findbugs is flagging up that falling through from one
case
to the next is generally not a good idea if there's any code in the first one (although sometimes it can be used to good effect). So when it sees the secondcase
and nobreak
, it reports the error.So for instance:
This is perfectly valid Java, but it probably doesn't do what the author intended: If
foo
is0
, all three of the functionsdoSomething
,doSomethingElse
, anddoSomeOtherThing
run (in that order). Iffoo
is1
, onlydoSomethingElse
anddoSomeOtherThing
run. Iffoo
is any other value, onlydoSomeOtherThing
runs.In contrast:
Here, only one of the functions will run, depending on the value of
foo
.Since it's a common coding error to forget the
break
, tools like Findbugs flag it up for you.There's a common use-case where you have multiple
case
statements in a row with no intervening code:There, we want to call
doSomething
iffoo
is0
or1
. Most tools won't flag this up as a possible coding error, because there's no code in thecase 0
prior to thecase 1
and this is a fairly common pattern.我把这些写成评论,但后来就看不到了。我正在把它们变成一个答案。这实际上是 TJCrowder 的答案的扩展。
您可以在此处找到导致 Findbugs 报告错误的相关规则。
您可以通过创建包含以下内容(例如
filter.xml
)的 xml 文件并使用-exclude filter.xml
选项运行该工具来防止 Findbugs 报告此类错误。请参阅Findbugs 上的过滤器。I wrote these as comments but then it's not visible. I'm turning them into an answer. This is actually an extension to T.J.Crowder's answer.
You can find the related rule that causes Findbugs to report an error here.
You can prevent Findbugs to report this kind of errors by creating an xml file with the following content, say
filter.xml
and running the tool with-exclude filter.xml
option. See filters on Findbugs.Switch 失败属于 Findbugs 的“狡猾代码”类别。我认为它只是标记 switch 语句中第一次出现的失败,以减少错误消息的数量。
Switch fall-throughs fall under the Findbugs category of "dodgy code". I think it only flags the first occurrence of a fall-through in a switch statement to cut down on the number of error messages.
如果没有中断,它们将相互陷入,因此如果
x == 0
您将遍历每个 case 语句块中的所有代码。 Findbugs 可能对错误有误,也可能是没有中断的错误条件,即case 0
中的某些内容导致case 1
中的某些内容中断。如果没有确切的代码和错误,我无法真正提供进一步的帮助。缺乏休息时间是故意的吗?
Without the break they will fall though into each other so if
x == 0
you'll go through all the code in every case statement block. Findbugs could either be wrong about the error, or it could be an error condition without the break, i.e. something incase 0
causes something incase 1
to break.Without the exact code and error I can't really help further. Is the lack of breaks deliberate?
通常错误分析工具不喜欢代码中的失败,因为在大多数情况下,用户只是忘记编写中断。
我不知道是否有办法专门禁用 FindBugs 的警告,但 Checkstyle 工具可以识别特殊注释,例如 /* Fallthrough */ 假设用户确实希望执行以下代码。添加这种注释也可以提高可读性。
http://checkstyle.sourceforge.net/config_coding.html#FallThrough
Java 代码约定还提到了fallthrough注释的使用。
http://www.oracle.com/technetwork/java/ javase/documentation/codeconventions-142311.html
Usually bug analysis tools don't like fallthrough in code because in most cases, the user has just forgotten to write the break.
I don't know if there is a way to specifically disable the warning with FindBugs but Checkstyle tool recognize special comments such as /* fallthrough */ to assume that the user really wants the following code to be executed. Putting this kind of comment also improves readability.
http://checkstyle.sourceforge.net/config_coding.html#FallThrough
Java code convention also mentions the use of fallthrough comment.
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html
如果您的案例没有中断,一旦触发案例,开关将遍历所有案例,直到找到案例的中断或结束。如果您有默认情况,如果您在这种情况下 foo 的开关值与任何其他情况不匹配,它将触发。
If your cases don't have break, once a case is trigger switch will go through all cases until it finds a break or end of the cases. If you have default case it will trigger if your switch value in this case foo does not match with any other cases.
是的,也可以使用不带中断的 switch 语句。仅有语法上的变化。例如:
像这样使用 switch 语句称为 enchaned switch
Yes it is possible to use switch statement without break also. Only changes will be in syntax. For instance:
Using switch statement like this is called as enchaned switch