[java] TooFewBranchesForSwitch - allow list of case constants (#5289)

This commit is contained in:
Juan Martín Sotuyo Dodero 2024-11-15 13:48:50 -06:00 committed by GitHub
commit 1ceae38880
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 1 deletions

View File

@ -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 * [#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 * [#5318](https://github.com/pmd/pmd/issues/5318): \[java] PreserveStackTraceRule: false-positive on Pattern Matching with instanceof
* java-performance * 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 * [#5314](https://github.com/pmd/pmd/issues/5314): \[java] InsufficientStringBufferDeclarationRule: Lack of handling for char type parameters
### 🚨 API Changes ### 🚨 API Changes

View File

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

View File

@ -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> ]]></code>
</test-code> </test-code>
</test-data> </test-data>