From 0c858b0a7bbbc051e428f7760701d736af592fdf Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 4 Oct 2024 16:03:21 +0200 Subject: [PATCH] [java] SwitchStmtsShouldHaveDefault should ignore patterns Fixes #4813 --- docs/pages/release_notes.md | 2 + .../resources/category/java/bestpractices.xml | 6 ++ .../xml/SwitchStmtsShouldHaveDefault.xml | 75 +++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 197b7a4aec..154ac73a28 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -32,6 +32,8 @@ The old rule names still work but are deprecated. ### 🐛 Fixed Issues * java * [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules +* java-bestpractices + * [#4813](https://github.com/pmd/pmd/issues/4813): \[java] SwitchStmtsShouldHaveDefault false positive with pattern matching * java-errorprone * [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault() diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 03b23ad1ca..aa965d9f67 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1168,12 +1168,18 @@ class SomeTestClass { easier to follow. This can be achieved by adding a `default` case, or, if the switch is on an enum type, by ensuring there is one switch branch for each enum constant. + + This rule doesn't consider Switch Statements, that use Pattern Matching, since for these the + compiler already ensures that all cases are covered. The same is true for Switch Expressions, + which are also not considered by this rule. 3 diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/SwitchStmtsShouldHaveDefault.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/SwitchStmtsShouldHaveDefault.xml index 758b97b43b..e28cfe5b9d 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/SwitchStmtsShouldHaveDefault.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/SwitchStmtsShouldHaveDefault.xml @@ -240,4 +240,79 @@ public enum GradeSystem { } ]]> + + + [java] SwitchStmtsShouldHaveDefault false positive with pattern matching #4813 + 0 + System.out.println("WARNING " + failure.message()); + case Success success -> System.out.println(success.message()); + } + } + public void test2(AcceptableResult result) { + switch (result) { + case ProviderWarning failure: System.out.println("Provider WARNING " + failure.message()); break; + case Warning failure: System.out.println("WARNING " + failure.message()); break; + case Success success: System.out.println(success.message()); break; + } + } + public void test3(AcceptableResult result) { + switch (result) { + case ProviderWarning failure -> System.out.println("Provider WARNING " + failure.message()); + case Success success -> System.out.println(success.message()); + default -> System.out.println("default case"); + } + } +} +]]> + + + + [java] SwitchStmtsShouldHaveDefault false positive with pattern matching #4813, example 2 + 0 + { return 1; } + case C c -> { return 2; } + case D d -> { return 3; } + // case B b -> { return 4; } // not explicitly necessary, but possible + } + } +} +]]> +