diff --git a/.all-contributorsrc b/.all-contributorsrc index 6d612e8df8..2542025f73 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -7453,7 +7453,8 @@ "avatar_url": "https://avatars.githubusercontent.com/u/16755668?v=4", "profile": "https://github.com/emouty", "contributions": [ - "code" + "code", + "bug" ] }, { diff --git a/docs/pages/pmd/projectdocs/credits.md b/docs/pages/pmd/projectdocs/credits.md index 0a1cd02cb0..b5a875fb83 100644 --- a/docs/pages/pmd/projectdocs/credits.md +++ b/docs/pages/pmd/projectdocs/credits.md @@ -918,7 +918,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d eant60
eant60

πŸ› ekkirala
ekkirala

πŸ› emersonmoura
emersonmoura

πŸ› - emouty
emouty

πŸ’» + emouty
emouty

πŸ’» πŸ› eugenepugach
eugenepugach

πŸ› fairy
fairy

πŸ› filiprafalowicz
filiprafalowicz

πŸ’» diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 6fb4e5bdef..67c0b20395 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -24,10 +24,12 @@ See [PR #5040](https://github.com/pmd/pmd/pull/5040) for details. ### 🌟 Rule Changes #### Changed Rules -* {% rule java/performance/TooFewBranchesForSwitch %} (Java Performance) doesn't report empty switches anymore. +* {% rule java/bestpractices/SwitchStmtsShouldHaveDefault %} (Java Best Practices) doesn't report empty switch statements anymore. To detect these, use {% rule java/codestyle/EmptyControlStatement %}. * {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) now also considers JUnit 5 and TestNG tests. * {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) now also considers JUnit 5 and TestNG tests. +* {% rule java/performance/TooFewBranchesForSwitch %} (Java Performance) doesn't report empty switches anymore. + To detect these, use {% rule java/codestyle/EmptyControlStatement %}. #### Renamed Rules * Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called @@ -47,6 +49,8 @@ The old rule names still work but are deprecated. * java * [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules * [#5261](https://github.com/pmd/pmd/issues/5261): \[java] Record patterns with empty deconstructor lists lead to NPE +* java-bestpractices + * [#4813](https://github.com/pmd/pmd/issues/4813): \[java] SwitchStmtsShouldHaveDefault false positive with pattern matching * java-codestyle * [#5253](https://github.com/pmd/pmd/issues/5253): \[java] BooleanGetMethodName: False-negatives with `Boolean` wrapper * java-design @@ -79,6 +83,7 @@ The old rule names still work but are deprecated. * [#5247](https://github.com/pmd/pmd/pull/5247): Fix #5030: \[java] SwitchDensity false positive with pattern matching - [Andreas Dangel](https://github.com/adangel) (@adangel) * [#5248](https://github.com/pmd/pmd/pull/5248): Fix #3362: \[java] ImplicitSwitchFallThrough should consider switch expressions - [Andreas Dangel](https://github.com/adangel) (@adangel) * [#5251](https://github.com/pmd/pmd/pull/5251): Fix #5249 and #5250: \[java] TooFewBranchesForSwitch ignore pattern matching and support switch expressions - [Andreas Dangel](https://github.com/adangel) (@adangel) +* [#5252](https://github.com/pmd/pmd/pull/5252): Fix #4813: \[java] SwitchStmtsShouldHaveDefault false positive with pattern matching - [Andreas Dangel](https://github.com/adangel) (@adangel) * [#5258](https://github.com/pmd/pmd/pull/5258): Ignore generated antlr classes in coverage reports - [Juan MartΓ­n Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod) * [#5264](https://github.com/pmd/pmd/pull/5264): Fix #5261: \[java] Fix NPE with empty pattern list - [ClΓ©ment Fournier](https://github.com/oowekyala) (@oowekyala) * [#5269](https://github.com/pmd/pmd/pull/5269): Fix #5253: \[java] Support Boolean wrapper class for BooleanGetMethodName rule - [Aryant Tripathi](https://github.com/Aryant-Tripathi) (@Aryant-Tripathi) diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 1a85431b37..b53d55d670 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -1168,12 +1168,20 @@ 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..eba1aef001 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 @@ -19,6 +19,19 @@ public class Foo { ]]> + + empty switch is ok + 0 + + + simple ok case 0 @@ -240,4 +253,128 @@ 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 + } + } +} +]]> + + + + With Record Patterns #4813 + 0 + System.out.println(a); + } + } +} +]]> + + + + Multiple Case Constants + 0 + System.out.println("a or b"); + case C -> System.out.println("c"); + } + String s = switch(e) { + case A,B -> "a or b"; + case C -> "c"; + }; + System.out.println(s); + } +} +]]> +