Fixed false positive when the first usage is an assignment to a field of the field (x.y = 5 instead of x = 5)

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5561 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Wouter Zelle
2007-10-19 11:53:28 +00:00
parent 279ce698fa
commit 0097f87ccc
2 changed files with 17 additions and 2 deletions

View File

@ -427,9 +427,23 @@ Ok, field used to aggregate values
public class Foo {
private int x;
int bar(int y) {
x = x + y;
x = x + y;
return x;
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Ok, setting values
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
private Person p = new Person();
public Foo(String name) {
p.name = name;
}
}
]]></code>
</test-code>

View File

@ -66,7 +66,8 @@ public class SingularField extends AbstractRule {
//Is the first usage in an assignment?
Node potentialStatement = primaryExpressionParent.jjtGetParent();
if (!isInAssignment(potentialStatement)) {
boolean assignmentToField = no.getImage().equals(location.getImage()); //Check the the assignment is not to a field inside the field object
if (!assignmentToField || !isInAssignment(potentialStatement)) {
violation = false;
break; //Optimization
} else {