Merge branch 'pr-1781'

This commit is contained in:
Juan Martín Sotuyo Dodero
2019-04-17 11:33:38 -03:00
3 changed files with 32 additions and 7 deletions

View File

@ -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 %}

View File

@ -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<Node> locations = initializedInConstructor(entry.getValue());
for (final Node location : locations) {
addViolation(data, location, decl.getImage());
}
}
return super.visit(node, data);
}
private boolean initializedInConstructor(List<NameOccurrence> usages) {
boolean initInConstructor = false;
private List<Node> initializedInConstructor(List<NameOccurrence> usages) {
final List<Node> 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;
}
}

View File

@ -8,6 +8,7 @@
clear rule violation
]]></description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
public class Foo {
static int x;
@ -28,6 +29,22 @@ public class Foo {
Foo(int y) {
x = y;
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
rule violated twice
]]></description>
<expected-problems>2</expected-problems>
<expected-linenumbers>4,5</expected-linenumbers>
<code><![CDATA[
public class Foo {
static int x;
Foo(int y) {
x = y;
x = y;
}
}
]]></code>
</test-code>