diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index bf631fd572..a1e5df6836 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -24,6 +24,12 @@ Being based on a proper Antlr grammar, CPD can: * ignore imports / libraries * honor [comment-based suppressions](pmd_userdocs_cpd.html#suppression) +### Modified Rules + +* The Java rule {% rule "java/errorprone/AssignmentToNonFinalStatic" %} (`java-errorprone`) will now report on each + assignment made within a constructor rather than on the field declaration. This makes it easier for developers to + find the offending statements. + ### Fixed Issues * go @@ -44,6 +50,7 @@ Being based on a proper Antlr grammar, CPD can: * [#1752](https://github.com/pmd/pmd/pull/1752): \[java] UseObjectForClearerAPI Only For Public - [Björn Kautler](https://github.com/Vampire) * [#1761](https://github.com/pmd/pmd/pull/1761): \[dart] \[cpd] Added CPD support for Dart - [Maikel Steneker](https://github.com/maikelsteneker) * [#1776](https://github.com/pmd/pmd/pull/1776): \[java] Show more detailed message when can't resolve field type - [Andrey Fomin](https://github.com/andrey-fomin) +* [#1781](https://github.com/pmd/pmd/pull/1781): \[java] Location change in AssignmentToNonFinalStatic - [Maikel Steneker](https://github.com/maikelsteneker) {% endtocmaker %} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java index 4870e31778..9c3f01631e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java @@ -4,6 +4,7 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -33,16 +34,16 @@ public class AssignmentToNonFinalStaticRule extends AbstractJavaRule { continue; } - if (initializedInConstructor(entry.getValue())) { - addViolation(data, decl.getNode(), decl.getImage()); + final List locations = initializedInConstructor(entry.getValue()); + for (final Node location : locations) { + addViolation(data, location, decl.getImage()); } } return super.visit(node, data); } - private boolean initializedInConstructor(List usages) { - boolean initInConstructor = false; - + private List initializedInConstructor(List usages) { + final List unsafeAssignments = new ArrayList<>(); for (NameOccurrence occ : usages) { // specifically omitting prefix and postfix operators as there are // legitimate usages of these with static fields, e.g. typesafe enum pattern. @@ -50,12 +51,12 @@ public class AssignmentToNonFinalStaticRule extends AbstractJavaRule { Node node = occ.getLocation(); Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class); if (constructor != null) { - initInConstructor = true; + unsafeAssignments.add(node); } } } - return initInConstructor; + return unsafeAssignments; } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AssignmentToNonFinalStatic.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AssignmentToNonFinalStatic.xml index 7f4c7844d1..a0ccf0047b 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AssignmentToNonFinalStatic.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AssignmentToNonFinalStatic.xml @@ -8,6 +8,7 @@ clear rule violation ]]> 1 + 4 + + + + 2 + 4,5 +