diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/SingularField.xml b/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/SingularField.xml index c0e34434cf..1bab9d87d1 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/SingularField.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/SingularField.xml @@ -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; } +} + ]]> + + + + 0 + diff --git a/pmd/src/net/sourceforge/pmd/rules/design/SingularField.java b/pmd/src/net/sourceforge/pmd/rules/design/SingularField.java index 459f4428c5..91ef8306da 100644 --- a/pmd/src/net/sourceforge/pmd/rules/design/SingularField.java +++ b/pmd/src/net/sourceforge/pmd/rules/design/SingularField.java @@ -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 {