From 16eafc89c2cdf8c0a370ab98d979d41d5d799540 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 27 Oct 2024 16:24:05 +0100 Subject: [PATCH 1/2] [java] TooFewBranchesForSwitch - allow list of case constants Fixes #5287 --- docs/pages/release_notes.md | 2 + .../resources/category/java/performance.xml | 2 +- .../xml/TooFewBranchesForSwitch.xml | 42 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index bc1a36ec7a..68f3742003 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -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 diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index a8e4c37142..8b2d4e20c5 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -629,7 +629,7 @@ Note: This rule was named TooFewBranchesForASwitchStatement before PMD 7.7.0. diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml index e603390ec6..2da0d99a82 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml @@ -136,6 +136,48 @@ public class SwitchWithRecordPattern { } } } +]]> + + + + #5287 [java] TooFewBranchesForSwitch false-positive with switch using list of case constants + 3 + 0 + 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; + } +} ]]> From ee7d6fed3e4795f4fcb8827d8fc6a6851bf191dc Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 7 Nov 2024 19:50:29 +0100 Subject: [PATCH 2/2] [java] TooFewBranchesForSwitch - add test case from comment on #5311 --- .../xml/TooFewBranchesForSwitch.xml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml index 2da0d99a82..4cbc9893c7 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/TooFewBranchesForSwitch.xml @@ -178,6 +178,26 @@ class Tester { return result; } } +]]> + + + + From #5311: Another example for list of case constants + 0 + "Hello"; + case E, F, G -> "World"; + }; + } + + enum Bar { + A, B, C, D, E, F, G + } +} ]]>