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
}
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.
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.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.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?
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.
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