From 0097f87ccc2d2a96c73056cf5b1199848c1bcab3 Mon Sep 17 00:00:00 2001 From: Wouter Zelle Date: Fri, 19 Oct 2007 11:53:28 +0000 Subject: [PATCH] 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 --- .../pmd/rules/design/xml/SingularField.xml | 16 +++++++++++++++- .../pmd/rules/design/SingularField.java | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) 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 {