[java] NonCaseLabelInSwitch - support switch expressions

Rename rule from NonCaseLabelInSwitchStatement

- as it applies to both switch statements and switch expressions
- extend the test cases to cover new java syntax
This commit is contained in:
Andreas Dangel 2024-10-05 11:19:53 +02:00
parent a0818d5ab2
commit 079eb238b9
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
6 changed files with 177 additions and 60 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/errorprone/NonCaseLabelInSwitch %} (Java Error Prone) has been renamed from `NonCaseLabelInSwitchStatement` as it also applies
to Switch Expressions.
The old rule names still work but are deprecated.
@ -43,7 +44,8 @@ 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.
* java-errorprone
* The old rule name `NonCaseLabelInSwitchStatement` has been deprecated. Use the new name {% rule java/errorprone/NonCaseLabelInSwitch %} 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

@ -2185,20 +2185,24 @@ public class Foo {
</example>
</rule>
<rule name="NonCaseLabelInSwitchStatement"
<rule name="NonCaseLabelInSwitchStatement" deprecated="true" ref="NonCaseLabelInSwitch" />
<rule name="NonCaseLabelInSwitch"
language="java"
since="1.5"
message="A non-case label was present in a switch statement"
message="A non-case label was present in a switch statement or expression"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#noncaselabelinswitchstatement">
<description>
A non-case label (e.g. a named break/continue label) was present in a switch statement.
A non-case label (e.g. a named break/continue label) was present in a switch statement or switch expression.
This is legal, but confusing. It is easy to mix up the case labels and the non-case labels.
Note: This rule was renamed from `NonCaseLabelInSwitchStatement` with PMD 7.7.0.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>//SwitchStatement//LabeledStatement</value>
<value>//(SwitchStatement|SwitchExpression)//LabeledStatement</value>
</property>
</properties>
<example>
@ -2208,7 +2212,6 @@ public class Foo {
switch (a) {
case 1:
// do something
break;
mylabel: // this is legal, but confusing!
break;
default:

View File

@ -243,7 +243,7 @@
<!-- <rule ref="category/java/errorprone.xml/MissingSerialVersionUID" /> -->
<rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/>
<!-- <rule ref="category/java/errorprone.xml/MoreThanOneLogger" /> -->
<rule ref="category/java/errorprone.xml/NonCaseLabelInSwitchStatement"/>
<rule ref="category/java/errorprone.xml/NonCaseLabelInSwitch"/>
<rule ref="category/java/errorprone.xml/NonStaticInitializer"/>
<!-- <rule ref="category/java/errorprone.xml/NullAssignment" /> -->
<rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode"/>

View File

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

View File

@ -0,0 +1,155 @@
<?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>label inside switch statement, not ok</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2: int y=8;
somelabel:
break;
default:
int j=8;
}
}
}
]]></code>
</test-code>
<test-code>
<description>label inside switch expression, not ok</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar(int x) {
x = switch (x) {
case 2: int y=8;
somelabel:
yield y;
default:
int j=8;
yield j;
};
}
}
]]></code>
</test-code>
<test-code>
<description>only cases in switch statement, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2: int y=8;
break;
default:
int j=8;
}
}
}
]]></code>
</test-code>
<test-code>
<description>only cases in switch expression, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
x = switch (x) {
case 2: int y=8;
yield y;
default:
int j=8;
yield j;
};
}
}
]]></code>
</test-code>
<test-code>
<description>label in switch statement with arrow syntax, not ok</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2 -> {
int y=8;
somelabel:
break;
}
default -> {
int j=8;
}
}
}
}
]]></code>
</test-code>
<test-code>
<description>label in switch expression with arrow syntax, not ok</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>5</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar(int x) {
x = switch (x) {
case 2 -> { int y=8;
somelabel:
yield y;
}
default -> {
int j=8;
yield j;
}
};
}
}
]]></code>
</test-code>
<test-code>
<description>only cases in switch stmt/expr with arrow syntax, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2 -> {
int y=8;
break;
}
default -> {
int j=8;
}
}
}
void barArrow(int x) {
x = switch (x) {
case 2 -> { int y=8;
yield y;
}
default -> {
int j=8;
yield j;
}
};
}
}
]]></code>
</test-code>
</test-data>

View File

@ -1,43 +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>label inside switch</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2: int y=8;
break;
somelabel:
break;
default:
int j=8;
}
}
}
]]></code>
</test-code>
<test-code>
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(int x) {
switch (x) {
case 2: int y=8;
break;
default:
int j=8;
}
}
}
]]></code>
</test-code>
</test-data>