[java] TooFewBranchesForSwitch - allow list of case constants
Fixes #5287
This commit is contained in:
@ -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
|
||||
|
||||
|
@ -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()]]
|
||||
]]>
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user