[java] Rename rule DefaultLabelNotLastInSwitch (#5255)

Merge pull request #5255 from adangel:rename-defaultlabelswitch
This commit is contained in:
Andreas Dangel
2024-10-24 14:10:16 +02:00
6 changed files with 152 additions and 68 deletions

View File

@ -40,8 +40,10 @@ See [PR #5040](https://github.com/pmd/pmd/pull/5040) for details.
* {% 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/performance/TooFewBranchesForSwitch %} (Java Performance) has been renamed from `TooFewBranchesForASwitchStatement`,
as it now applies to Switch Expressions as well.
* {% rule java/bestpractices/DefaultLabelNotLastInSwitch %} (Java Best Practices) has been renamed from `DefaultLabelNotLastInSwitch`, as it also
applies to Switch Expressions.
* {% rule java/performance/TooFewBranchesForSwitch %} (Java Performance) has been renamed from `TooFewBranchesForASwitchStatement`, as it also
applies to Switch Expressions.
The old rule names still work but are deprecated.
@ -70,10 +72,10 @@ 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.
* java-performance
* The old rule name `TooFewBranchesForASwitchStatement` has been deprecated. Use the new name {% rule java/performance/TooFewBranchesForSwitch %} instead.
### ✨ Merged pull requests
* [#4965](https://github.com/pmd/pmd/pull/4965): Fix #4532: \[java] Rename JUnit rules with overly restrictive names - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
* [#5040](https://github.com/pmd/pmd/pull/5040): \[cpp] Ignore literals and ignore identifiers capability to C++ CPD - [Jakub Dupak](https://github.com/jdupak) (@jdupak)
@ -84,6 +86,7 @@ The old rule names still work but are deprecated.
* [#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)
* [#5255](https://github.com/pmd/pmd/pull/5255): \[java] Rename rule DefaultLabelNotLastInSwitch - [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)

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>