From 07840cace2d8b375bbc22331d864d8a0b9375b27 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Tue, 8 Oct 2024 17:26:18 +0200 Subject: [PATCH] [java] Rename rule SwitchStmtsShouldHaveDefault to NonExhaustiveSwitch Fixes #4286 --- docs/pages/release_notes.md | 21 +++--- .../resources/category/java/bestpractices.xml | 68 ++++++++++--------- .../resources/rulesets/java/quickstart.xml | 2 +- ...Test.java => NonExhaustiveSwitchTest.java} | 2 +- ...aveDefault.xml => NonExhaustiveSwitch.xml} | 0 5 files changed, 49 insertions(+), 44 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/{SwitchStmtsShouldHaveDefaultTest.java => NonExhaustiveSwitchTest.java} (79%) rename pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/{SwitchStmtsShouldHaveDefault.xml => NonExhaustiveSwitch.xml} (100%) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 197b7a4aec..aac6ab6801 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -17,21 +17,23 @@ This is a {{ site.pmd.release_type }} release. ### 🌟 Rule Changes #### Renamed Rules -Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called -after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG. - -* {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} (Java Best Practices) has been renamed from `JUnitAssertionsShouldIncludeMessage`. -* {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} (Java Best Practices) has been renamed from `JUnitTestContainsTooManyAsserts`. -* {% rule java/bestpractices/UnitTestShouldIncludeAssert %} (Java Best Practices) has been renamed from `JUnitTestsShouldIncludeAssert`. -* {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseAfterAnnotation`. -* {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseBeforeAnnotation`. -* {% rule java/bestpractices/UnitTestShouldUseTestAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseTestAnnotation`. +* Several rules for unit testing have been renamed to better reflect their actual scope. Lots of them were called + after JUnit / JUnit 4, even when they applied to JUnit 5 and / or TestNG. + * {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} (Java Best Practices) has been renamed from `JUnitAssertionsShouldIncludeMessage`. + * {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} (Java Best Practices) has been renamed from `JUnitTestContainsTooManyAsserts`. + * {% rule java/bestpractices/UnitTestShouldIncludeAssert %} (Java Best Practices) has been renamed from `JUnitTestsShouldIncludeAssert`. + * {% rule java/bestpractices/UnitTestShouldUseAfterAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseAfterAnnotation`. + * {% rule java/bestpractices/UnitTestShouldUseBeforeAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseBeforeAnnotation`. + * {% rule java/bestpractices/UnitTestShouldUseTestAnnotation %} (Java Best Practices) has been renamed from `JUnit4TestShouldUseTestAnnotation`. +* {%rule java/bestpractices/NonExhaustiveSwitch %} (Java Best Practices) has been renamed from `SwitchStmtsShouldHaveDefault`. 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 + * [#4286](https://github.com/pmd/pmd/issues/4286): \[java] Rename rule SwitchStmtsShouldHaveDefault to NonExhaustiveSwitch * java-errorprone * [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault() @@ -43,6 +45,7 @@ The old rule names still work but are deprecated. * The old rule name `JUnitAssertionsShouldIncludeMessage` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestAssertionsShouldIncludeMessage %} instead. * The old rule name `JUnitTestContainsTooManyAsserts` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestContainsTooManyAsserts %} instead. * The old rule name `JUnitTestsShouldIncludeAssert` has been deprecated. Use the new name {% rule java/bestpractices/UnitTestShouldIncludeAssert %} instead. + * The old rule name `SwitchStmtsShouldHaveDefault` has been deprecated. USe the new name {%rule java/bestpractices/NonExhaustiveSwitch %} instead. ### ✨ Merged pull requests diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 03b23ad1ca..14ae96fcdc 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -904,6 +904,40 @@ public class SecureSystem { + + + Switch statements should be exhaustive, to make their control flow + 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. + + 3 + + + + + + + + + + - - - Switch statements should be exhaustive, to make their control flow - 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. - - 3 - - - - - - - - - + + @@ -37,7 +38,6 @@ - diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/SwitchStmtsShouldHaveDefaultTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/NonExhaustiveSwitchTest.java similarity index 79% rename from pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/SwitchStmtsShouldHaveDefaultTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/NonExhaustiveSwitchTest.java index 1e413f1bde..2d7b18f754 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/SwitchStmtsShouldHaveDefaultTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/NonExhaustiveSwitchTest.java @@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices; import net.sourceforge.pmd.test.PmdRuleTst; -class SwitchStmtsShouldHaveDefaultTest extends PmdRuleTst { +class NonExhaustiveSwitchTest extends PmdRuleTst { // no additional unit tests } 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/NonExhaustiveSwitch.xml similarity index 100% rename from pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/SwitchStmtsShouldHaveDefault.xml rename to pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/NonExhaustiveSwitch.xml