[java] TooFewBranchesForSwitch - allow list of case constants (#5289)
This commit is contained in:
commit
1ceae38880
@ -29,6 +29,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
* [#5083](https://github.com/pmd/pmd/issues/5083): \[java] UnusedPrivateMethod false positive when method reference has no target type
|
||||
* [#5318](https://github.com/pmd/pmd/issues/5318): \[java] PreserveStackTraceRule: false-positive on Pattern Matching with instanceof
|
||||
* java-performance
|
||||
* [#5287](https://github.com/pmd/pmd/issues/5287): \[java] TooFewBranchesForSwitch false-positive with switch using list of case constants
|
||||
* [#5314](https://github.com/pmd/pmd/issues/5314): \[java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters
|
||||
|
||||
### 🚨 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,68 @@ 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-code>
|
||||
<description>From #5311: Another example for list of case constants</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
|
||||
String foo(Bar bar) {
|
||||
return switch (bar) {
|
||||
case A, B, C, D -> "Hello";
|
||||
case E, F, G -> "World";
|
||||
};
|
||||
}
|
||||
|
||||
enum Bar {
|
||||
A, B, C, D, E, F, G
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Loading…
x
Reference in New Issue
Block a user