Merge branch 'pr-1520'
This commit is contained in:
@ -14,6 +14,13 @@ This is a {{ site.pmd.release_type }} release.
|
|||||||
|
|
||||||
### New and noteworthy
|
### New and noteworthy
|
||||||
|
|
||||||
|
#### New Rules
|
||||||
|
|
||||||
|
* The new Java rule {% rule "java/bestpractices/ForLoopVariableCount" %} (`java-bestpractices`) checks for
|
||||||
|
the number of control variables in a for-loop. Having a lot of control variables makes it harder to understand
|
||||||
|
what the loop does. The maximum allowed number of variables is by default 1 and can be configured by a
|
||||||
|
property.
|
||||||
|
|
||||||
#### Modified Rules
|
#### Modified Rules
|
||||||
|
|
||||||
* The Java rule {% rule "java/codestyle/LocalVariableCouldBeFinal" %} (`java-codestyle`) has a new
|
* The Java rule {% rule "java/codestyle/LocalVariableCouldBeFinal" %} (`java-codestyle`) has a new
|
||||||
@ -24,6 +31,7 @@ This is a {{ site.pmd.release_type }} release.
|
|||||||
|
|
||||||
* java-bestpractices
|
* java-bestpractices
|
||||||
* [#658](https://github.com/pmd/pmd/issues/658): \[java] OneDeclarationPerLine: False positive for loops
|
* [#658](https://github.com/pmd/pmd/issues/658): \[java] OneDeclarationPerLine: False positive for loops
|
||||||
|
* [#1519](https://github.com/pmd/pmd/issues/1519): \[java] New rule: ForLoopVariableCount
|
||||||
* java-codestyle
|
* java-codestyle
|
||||||
* [#1513](https://github.com/pmd/pmd/issues/1513): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop
|
* [#1513](https://github.com/pmd/pmd/issues/1513): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop
|
||||||
* java-errorprone
|
* java-errorprone
|
||||||
@ -40,6 +48,7 @@ This is a {{ site.pmd.release_type }} release.
|
|||||||
* [#1503](https://github.com/pmd/pmd/pull/1503): \[java] Fix for ReturnFromFinallyBlock false-positives - [RishabhDeep Singh](https://github.com/rishabhdeepsingh)
|
* [#1503](https://github.com/pmd/pmd/pull/1503): \[java] Fix for ReturnFromFinallyBlock false-positives - [RishabhDeep Singh](https://github.com/rishabhdeepsingh)
|
||||||
* [#1514](https://github.com/pmd/pmd/pull/1514): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop - [Kris Scheibe](https://github.com/kris-scheibe)
|
* [#1514](https://github.com/pmd/pmd/pull/1514): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop - [Kris Scheibe](https://github.com/kris-scheibe)
|
||||||
* [#1516](https://github.com/pmd/pmd/pull/1516): \[java] OneDeclarationPerLine: Don't report multiple variables in a for statement. - [Kris Scheibe](https://github.com/kris-scheibe)
|
* [#1516](https://github.com/pmd/pmd/pull/1516): \[java] OneDeclarationPerLine: Don't report multiple variables in a for statement. - [Kris Scheibe](https://github.com/kris-scheibe)
|
||||||
|
* [#1520](https://github.com/pmd/pmd/pull/1520): \[java] New rule: ForLoopVariableCount: check the number of control variables in a for loop - [Kris Scheibe](https://github.com/kris-scheibe)
|
||||||
* [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard)
|
* [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard)
|
||||||
|
|
||||||
{% endtocmaker %}
|
{% endtocmaker %}
|
||||||
|
@ -382,6 +382,33 @@ public class MyClass {
|
|||||||
</example>
|
</example>
|
||||||
</rule>
|
</rule>
|
||||||
|
|
||||||
|
<rule name="ForLoopVariableCount"
|
||||||
|
language="java"
|
||||||
|
since="6.11.0"
|
||||||
|
message="Too many control variables in the 'for' statement"
|
||||||
|
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||||
|
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#forloopvariablecount">
|
||||||
|
<description>
|
||||||
|
Having a lot of control variables in a 'for' loop makes it harder to see what range of values
|
||||||
|
the loop iterates over. By default this rule allows a regular 'for' loop with only one variable.
|
||||||
|
</description>
|
||||||
|
<priority>3</priority>
|
||||||
|
<properties>
|
||||||
|
<property name="maximumVariables" type="Integer"
|
||||||
|
description="A regular for statement will have 1 control variable" min="0" max="100" value="1"/>
|
||||||
|
<property name="xpath">
|
||||||
|
<value>//ForInit/LocalVariableDeclaration[count(VariableDeclarator) > $maximumVariables]</value>
|
||||||
|
</property>
|
||||||
|
</properties>
|
||||||
|
<example>
|
||||||
|
<![CDATA[
|
||||||
|
// this will be reported with the default setting of at most one control variable in a for loop
|
||||||
|
for (int i = 0, j = 0; i < 10; i++, j += 2) {
|
||||||
|
foo();
|
||||||
|
]]>
|
||||||
|
</example>
|
||||||
|
</rule>
|
||||||
|
|
||||||
<rule name="GuardLogStatement"
|
<rule name="GuardLogStatement"
|
||||||
language="java"
|
language="java"
|
||||||
since="5.1.0"
|
since="5.1.0"
|
||||||
|
@ -787,7 +787,6 @@ for (int i = 0; i < 42; i++)
|
|||||||
</example>
|
</example>
|
||||||
</rule>
|
</rule>
|
||||||
|
|
||||||
|
|
||||||
<rule name="FormalParameterNamingConventions"
|
<rule name="FormalParameterNamingConventions"
|
||||||
since="6.6.0"
|
since="6.6.0"
|
||||||
message="The {0} name ''{1}'' doesn''t match ''{2}''"
|
message="The {0} name ''{1}'' doesn''t match ''{2}''"
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sourceforge.pmd.lang.java.rule.bestpractices;
|
||||||
|
|
||||||
|
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||||
|
|
||||||
|
public class ForLoopVariableCountTest extends PmdRuleTst {
|
||||||
|
// no additional unit tests
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
<?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>default: valid case</description>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
void foo() {
|
||||||
|
for (int i=0; i<42; i++) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>default: failing case</description>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
void foo() {
|
||||||
|
for (int i=0, j=0; i<42; i++, j++) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>custom count: valid case</description>
|
||||||
|
<rule-property name="maximumVariables">2</rule-property>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
void foo() {
|
||||||
|
for (int i=0, j=0; i<42; i++, j++) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>custom count: valid (but useless) case</description>
|
||||||
|
<rule-property name="maximumVariables">0</rule-property>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
void foo() {
|
||||||
|
for (;;) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
<test-code>
|
||||||
|
<description>custom count: failing case</description>
|
||||||
|
<rule-property name="maximumVariables">2</rule-property>
|
||||||
|
<expected-problems>1</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
public class Foo {
|
||||||
|
void foo() {
|
||||||
|
for (int i=0, j=0, k=0; i<42; i++, j++, k++) {
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
</test-data>
|
Reference in New Issue
Block a user