diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 74549e2297..cf0b330b6c 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -14,10 +14,18 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy +#### Modified Rules + +* The Java rule {% rule "java/codestyle/LocalVariableCouldBeFinal" %} (`java-codestyle`) has a new + property `ignoreForEachDecl`, which is by default disabled. The new property allows for ignoring + non-final loop variables in a for-each statement. + ### Fixed Issues * java-bestpractices * [#658](https://github.com/pmd/pmd/issues/658): \[java] OneDeclarationPerLine: False positive for loops +* java-codestyle + * [#1513](https://github.com/pmd/pmd/issues/1513): \[java] LocalVariableCouldBeFinal: allow excluding the variable in a for-each loop * java-errorprone * [#1035](https://github.com/pmd/pmd/issues/1035): \[java] ReturnFromFinallyBlock: False positive on lambda expression in finally block @@ -26,6 +34,7 @@ This is a {{ site.pmd.release_type }} release. ### External Contributions * [#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) * [#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) * [#1521](https://github.com/pmd/pmd/pull/1521): \[java] Upgrade to ASM7 for JDK 11 support - [Mark Pritchard](https://github.com/markpritchard) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LocalVariableCouldBeFinalRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LocalVariableCouldBeFinalRule.java index 7bf3bb424a..02678cf884 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LocalVariableCouldBeFinalRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/LocalVariableCouldBeFinalRule.java @@ -4,22 +4,36 @@ package net.sourceforge.pmd.lang.java.rule.codestyle; +import static net.sourceforge.pmd.properties.PropertyFactory.booleanProperty; + import java.util.List; import java.util.Map; +import net.sourceforge.pmd.lang.java.ast.ASTForStatement; import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration; import net.sourceforge.pmd.lang.java.rule.performance.AbstractOptimizationRule; import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; import net.sourceforge.pmd.lang.symboltable.NameOccurrence; import net.sourceforge.pmd.lang.symboltable.Scope; +import net.sourceforge.pmd.properties.PropertyDescriptor; public class LocalVariableCouldBeFinalRule extends AbstractOptimizationRule { + private static final PropertyDescriptor IGNORE_FOR_EACH = + booleanProperty("ignoreForEachDecl").defaultValue(false).desc("Ignore non-final loop variables in a for-each statement.").build(); + + public LocalVariableCouldBeFinalRule() { + definePropertyDescriptor(IGNORE_FOR_EACH); + } + @Override public Object visit(ASTLocalVariableDeclaration node, Object data) { if (node.isFinal()) { return data; } + if (getProperty(IGNORE_FOR_EACH) && node.jjtGetParent() instanceof ASTForStatement) { + return data; + } Scope s = node.getScope(); Map> decls = s.getDeclarations(VariableNameDeclaration.class); for (Map.Entry> entry : decls.entrySet()) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableCouldBeFinal.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableCouldBeFinal.xml index 37db064a4b..055e2ba1ee 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableCouldBeFinal.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/LocalVariableCouldBeFinal.xml @@ -4,9 +4,7 @@ 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"> - + TEST1 1 - + TEST2 0 - + TEST3 0 - + TEST4 0 - + TEST5 2 - + TEST6 0 - + TEST7 0 - + TEST8 0 - + TEST9 1 - + Bug 2614040 : false + if a += assignment operator is used inside a method call. 0 + + Parameter to ignore non-final variables in for each loops (see #1513). + true + 0 + + + + By default, for-each loops should be flagged (see #1513). + 1 + 3 + + + + Normal for-loops should not be flagged + 0 + +