resources.report.rules.pmd.MissingBreakInSwitch.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
MissingBreakInSwitch
MissingBreakInSwitch
Switch statements without break or return statements for each case option may indicate problematic behaviour. Empty cases are ignored as these indicate an intentional fall-through.
//SwitchStatement
[(count(.//BreakStatement)
+ count(BlockStatement//Statement/ReturnStatement)
+ count(BlockStatement//Statement/ContinueStatement)
+ count(BlockStatement//Statement/ThrowStatement)
+ count(BlockStatement//Statement/IfStatement[@Else='true' and Statement[2][ReturnStatement|ContinueStatement|ThrowStatement]]/Statement[1][ReturnStatement|ContinueStatement|ThrowStatement])
+ count(SwitchLabel[name(following-sibling::node()) = 'SwitchLabel'])
+ count(SwitchLabel[count(following-sibling::node()) = 0])
< count (SwitchLabel))]
Example(s):
public void bar(int status) {
switch(status) {
case CANCELLED:
doCancelled();
// break; hm, should this be commented out?
case NEW:
doNew();
// is this really a fall-through?
case REMOVED:
doRemoved();
// what happens if you add another case after this one?
case OTHER: // empty case - this is interpreted as an intentional fall-through
case ERROR:
doErrorHandling();
break;
}
}