[java] TooFewBranchesForSwitch - allow list of case constants

Fixes #5287
This commit is contained in:
Andreas Dangel
2024-10-27 16:24:05 +01:00
parent 9c098b2531
commit 16eafc89c2
3 changed files with 45 additions and 1 deletions

View File

@ -15,6 +15,8 @@ This is a {{ site.pmd.release_type }} release.
### 🚀 New and noteworthy
### 🐛 Fixed Issues
* java-performance
* [#5287](https://github.com/pmd/pmd/issues/5287): \[java] TooFewBranchesForSwitch false-positive with switch using list of case constants
### 🚨 API Changes

View File

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

View File

@ -136,6 +136,48 @@ public class SwitchWithRecordPattern {
}
}
}
]]></code>
</test-code>
<test-code>
<description>#5287 [java] TooFewBranchesForSwitch false-positive with switch using list of case constants</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
enum SomeEnum { A, B, C, D }
class Tester {
int checkSwitchExpression(SomeEnum someEnumValue) {
return switch(someEnumValue) {
case A, B, C -> 1;
default -> 0;
};
}
int checkSwitchStatement(SomeEnum someEnumValue) {
int result;
switch(someEnumValue) {
case A, B, C -> { result = 1; }
default -> { result = 0; }
}
return result;
}
int checkSwitchExpressionGroup(SomeEnum someEnumValue) {
return switch(someEnumValue) {
case A, B, C: yield 1;
default: yield 0;
};
}
int checkSwitchStatementGroup(SomeEnum someEnumValue) {
int result;
switch(someEnumValue) {
case A, B, C: result = 1; break;
default: result = 0; break;
}
return result;
}
}
]]></code>
</test-code>
</test-data>