[java] TooFewBranchesForSwitch should consider Switch Expressions

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

View File

@ -36,7 +36,7 @@ The old rule names still work but are deprecated.
* [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault() * [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault()
* java-performance * java-performance
* [#5249](https://github.com/pmd/pmd/issues/5249): \[java] TooFewBranchesForASwitchStatement false positive for Pattern Matching * [#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 * [#5250](https://github.com/pmd/pmd/issues/5250): \[java] TooFewBranchesForASwitchStatement should consider Switch Expressions
### 🚨 API Changes ### 🚨 API Changes
* java-bestpractices * java-bestpractices

View File

@ -624,7 +624,7 @@ if-else statement to increase code readability.
<property name="xpath"> <property name="xpath">
<value> <value>
<![CDATA[ <![CDATA[
//SwitchStatement[ (count(*) - 1) < $minimumNumberCaseForASwitch ] //(SwitchStatement | SwitchExpression)[ (count(*) - 1) < $minimumNumberCaseForASwitch ]
(: exclude pattern matching :) (: exclude pattern matching :)
[not(*/SwitchLabel/TypePattern)] [not(*/SwitchLabel/TypePattern)]
]]> ]]>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code> <test-code>
<description>Only one case, this is useless</description> <description>Switch Statement with only one case, not ok</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> <rule-property name="minimumNumberCaseForASwitch">3</rule-property>
<expected-problems>1</expected-problems> <expected-problems>1</expected-problems>
<code><![CDATA[ <code><![CDATA[
@ -22,6 +22,24 @@ public class DumbSwitch {
]]></code> ]]></code>
</test-code> </test-code>
<test-code>
<description>Switch Expression with only one case, not ok</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class DumbSwitch {
public String foo(int i) {
return switch (i) {
case 0:
{
yield "I am a fish.";
}
};
}
}
]]></code>
</test-code>
<test-code> <test-code>
<description>Even two branches is not enough for a switch statement</description> <description>Even two branches is not enough for a switch statement</description>
<rule-property name="minimumNumberCaseForASwitch">3</rule-property> <rule-property name="minimumNumberCaseForASwitch">3</rule-property>