[java] Rename rule DefaultLabelNotLastInSwitch

- as it applies to both switch statements and switch expressions
- extend the test cases to cover new java syntax

Note: For patterns in switch, the java compiler already makes sure, that
default is the last case.
This commit is contained in:
Andreas Dangel 2024-10-05 11:00:13 +02:00
parent a0818d5ab2
commit b3157825d8
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
6 changed files with 157 additions and 75 deletions

View File

@ -17,15 +17,16 @@ 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/DefaultLabelNotLastInSwitch %} (Java Best Practices) has been renamed from `DefaultLabelNotLastInSwitch`, as it also
applies to Switch Expressions.
The old rule names still work but are deprecated.
@ -43,7 +44,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 `DefaultLabelNotLastInSwitch` has been deprecated. Use the new name {% rule java/bestpractices/DefaultLabelNotLastInSwitch %} instead.
### ✨ Merged pull requests
* [#4965](https://github.com/pmd/pmd/pull/4965): \[java] Rename JUnit rules with overly restrictive names - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)

View File

@ -457,14 +457,18 @@ public interface YetAnotherConstantInterface {
</example>
</rule>
<rule name="DefaultLabelNotLastInSwitchStmt"
<rule name="DefaultLabelNotLastInSwitchStmt" deprecated="true" ref="DefaultLabelNotLastInSwitch" />
<rule name="DefaultLabelNotLastInSwitch"
language="java"
since="1.5"
message="The default label should be the last label in a switch statement"
message="The default label should be the last label in a switch statement or expression"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#defaultlabelnotlastinswitchstmt">
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#defaultlabelnotlastinswitch">
<description>
By convention, the default label should be the last label in a switch statement.
By convention, the default label should be the last label in a switch statement or switch expression.
Note: This rule has been renamed from "DefaultLabelNotLastInSwitchStmt" with PMD 7.7.0.
</description>
<priority>3</priority>
<properties>

View File

@ -18,7 +18,7 @@
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP"/>
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
<rule ref="category/java/bestpractices.xml/ConstantsInInterface"/>
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt"/>
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitch"/>
<rule ref="category/java/bestpractices.xml/DoubleBraceInitialization"/>
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
<!-- <rule ref="category/java/bestpractices.xml/ForLoopVariableCount" /> -->

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.test.PmdRuleTst;
class DefaultLabelNotLastInSwitchStmtTest extends PmdRuleTst {
class DefaultLabelNotLastInSwitchTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
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">
<test-code>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
default:
break;
}
}
}
]]></code>
</test-code>
<test-code>
<description>bad</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
default:
break;
case 2:
break;
}
}
}
]]></code>
</test-code>
<test-code>
<description>ok, no default</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
case 2:
break;
}
}
}
]]></code>
</test-code>
<test-code>
<description>switch expression, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
x = switch(x) {
case 1: yield 1;
default: yield -1;
};
}
}
]]></code>
</test-code>
<test-code>
<description>switch expression, not ok</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
x = switch(x) {
case 1: yield 1;
default: yield -1;
case 2: yield 2;
};
}
}
]]></code>
</test-code>
<test-code>
<description>switch arrow, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1 -> System.out.println("a");
default -> System.out.println("default");
}
}
}
]]></code>
</test-code>
<test-code>
<description>switch arrow, not ok</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1 -> System.out.println("a");
default -> System.out.println("default");
case 2 -> System.out.println("b");
}
}
}
]]></code>
</test-code>
<test-code>
<description>switch with pattern and default case, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void barPattern(Object x) {
x = switch(x) {
case Integer i: yield 1;
case null, default: yield -1;
};
}
}
]]></code>
</test-code>
</test-data>

View File

@ -1,59 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
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">
<test-code>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
default:
break;
}
}
}
]]></code>
</test-code>
<test-code>
<description>bad</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
default:
break;
case 2:
break;
}
}
}
]]></code>
</test-code>
<test-code>
<description>ok, no default</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch(x) {
case 1:
break;
case 2:
break;
}
}
}
]]></code>
</test-code>
</test-data>