[java] TooFewBranchesForSwitch: report empty switch

This fixes a false negative
This commit is contained in:
Andreas Dangel
2024-10-24 12:38:45 +02:00
parent 6f5af6e585
commit d9d6b57f2a
2 changed files with 14 additions and 1 deletions

View File

@ -631,7 +631,7 @@ Note: This rule was named TooFewBranchesForASwitchStatement before PMD 7.7.0.
//(SwitchStatement | SwitchExpression) //(SwitchStatement | SwitchExpression)
[ (count(*) - 1) < $minimumNumberCaseForASwitch ] [ (count(*) - 1) < $minimumNumberCaseForASwitch ]
(: only consider if no pattern matching is used :) (: only consider if no pattern matching is used :)
[*/SwitchLabel[@PatternLabel = false()]] [not(*/SwitchLabel[@PatternLabel = true()])]
]]> ]]>
</value> </value>
</property> </property>

View File

@ -4,6 +4,19 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Switch Statement with no case, not ok</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class DumbSwitch {
public void foo(int i) {
switch (i) { }
}
}
]]></code>
</test-code>
<test-code> <test-code>
<description>Switch Statement with only one case, not ok</description> <description>Switch Statement with only one case, not ok</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> <rule-property name="minimumNumberCaseForASwitch">3</rule-property>