From d9d6b57f2a32d9ff1f0cbf55e9960b4dad26e0c8 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 24 Oct 2024 12:38:45 +0200 Subject: [PATCH] [java] TooFewBranchesForSwitch: report empty switch This fixes a false negative --- .../main/resources/category/java/performance.xml | 2 +- .../performance/xml/TooFewBranchesForSwitch.xml | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index a8e4c37142..f2e7eef431 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -631,7 +631,7 @@ Note: This rule was named TooFewBranchesForASwitchStatement before PMD 7.7.0. //(SwitchStatement | SwitchExpression) [ (count(*) - 1) < $minimumNumberCaseForASwitch ] (: only consider if no pattern matching is used :) - [*/SwitchLabel[@PatternLabel = false()]] + [not(*/SwitchLabel[@PatternLabel = true()])] ]]> 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 502c470bc2..239aebbb12 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 @@ -4,6 +4,19 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> + + Switch Statement with no case, not ok + 3 + 1 + + + Switch Statement with only one case, not ok 3