[java] TooFewBranchesForSwitch should ignore Pattern Matching

Fixes #5249
This commit is contained in:
Andreas Dangel 2024-10-04 16:17:27 +02:00
parent a0818d5ab2
commit 90f436fd28
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 27 additions and 0 deletions

View File

@ -34,6 +34,9 @@ The old rule names still work but are deprecated.
* [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules
* java-errorprone
* [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault()
* java-performance
* [#5249](https://github.com/pmd/pmd/issues/5249): \[java] TooFewBranchesForASwitchStatement false positive for Pattern Matching
* [#....](https://github.com/pmd/pmd/issues/....): \[java] TooFewBranchesForASwitchStatement should consider Switch Expressions
### 🚨 API Changes
* java-bestpractices

View File

@ -625,6 +625,8 @@ if-else statement to increase code readability.
<value>
<![CDATA[
//SwitchStatement[ (count(*) - 1) < $minimumNumberCaseForASwitch ]
(: exclude pattern matching :)
[not(*/SwitchLabel/TypePattern)]
]]>
</value>
</property>

View File

@ -69,4 +69,26 @@ public class ValidSwitch {
}
]]></code>
</test-code>
<test-code>
<description>[java] TooFewBranchesForASwitchStatement false positive for Pattern Matching</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
sealed interface S permits A {}
final class A implements S {}
public class Sample {
public void simpleSwitchStatment(S s) {
switch(s) {
case A a -> System.out.println("a");
}
}
public void simpleSwitchExpression(S s) {
String result = switch(s) {
case A a -> "a";
};
}
}
]]></code>
</test-code>
</test-data>