diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 168174a37c..44441f6c78 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -3,6 +3,7 @@ Fixed bug 1618858 - PMD no longer raises an exception on XPath like '//Condition Fixed bug 1626232 - Commons logging rules (ProperLogger and UseCorrectExceptionLogging) now catch more cases Fixed bugs 1626201 & 1633683 - BrokenNullCheck now catches more cases Fixed bug 1626715 - UseAssertSameInsteadOfAssertTrue now correctly checks classes which contain the null constant +Fixed bug 1531216 - ImmutableField. NameOccurrence.isSelfAssignment now recognizes this.x++ as a self assignment Applied patch 1612455 - RFE 1411022 CompareObjectsWithEquals now catches the case where comparison is against new Object XPath rules are now chained together for an extra speedup in processing diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/ImmutableField.xml b/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/ImmutableField.xml index 2cb18838dc..7b128101a5 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/ImmutableField.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/design/xml/ImmutableField.xml @@ -240,4 +240,25 @@ public class Foo { } ]]> + + + 0 + + \ No newline at end of file diff --git a/pmd/regress/test/net/sourceforge/pmd/symboltable/NameOccurrencesTest.java b/pmd/regress/test/net/sourceforge/pmd/symboltable/NameOccurrencesTest.java index 3081ed06c1..bf478df684 100644 --- a/pmd/regress/test/net/sourceforge/pmd/symboltable/NameOccurrencesTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/symboltable/NameOccurrencesTest.java @@ -52,6 +52,18 @@ public class NameOccurrencesTest extends STBBaseTst { assertEquals("b", ((NameOccurrence) occs.getNames().get(0)).getImage()); assertEquals("x", ((NameOccurrence) occs.getNames().get(1)).getImage()); } + + public void testIsSelfAssignment(){ + parseCode(TEST5); + List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class); + NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(2)); + assertTrue(((NameOccurrence) occs.getNames().get(0)).isSelfAssignment()); + + parseCode(TEST6); + nodes = acu.findChildrenOfType(ASTPrimaryExpression.class); + occs = new NameFinder((ASTPrimaryExpression) nodes.get(2)); + assertTrue(((NameOccurrence) occs.getNames().get(0)).isSelfAssignment()); + } public static final String TEST1 = "public class Foo {" + PMD.EOL + @@ -80,4 +92,32 @@ public class NameOccurrencesTest extends STBBaseTst { " b.x = 2;" + PMD.EOL + " }" + PMD.EOL + "}"; + + public static final String TEST5 = + "public class Foo{" + PMD.EOL + + " private int counter;" + PMD.EOL + + " private Foo(){" + PMD.EOL + + " counter = 0;" + PMD.EOL + + " }" + PMD.EOL + + " private int foo(){" + PMD.EOL + + " if (++counter < 3) {" + PMD.EOL + + " return 0;" + PMD.EOL + + " }" + PMD.EOL + + " return 1;" + PMD.EOL + + " }" + PMD.EOL + + "}"; + + public static final String TEST6 = + "public class Foo{" + PMD.EOL + + " private int counter;" + PMD.EOL + + " private Foo(){" + PMD.EOL + + " counter = 0;" + PMD.EOL + + " }" + PMD.EOL + + " private int foo(){" + PMD.EOL + + " if (++this.counter < 3) {" + PMD.EOL + + " return 0;" + PMD.EOL + + " }" + PMD.EOL + + " return 1;" + PMD.EOL + + " }" + PMD.EOL + + "}"; } diff --git a/pmd/src/net/sourceforge/pmd/symboltable/NameOccurrence.java b/pmd/src/net/sourceforge/pmd/symboltable/NameOccurrence.java index 632d4c8e4b..bf16bf3329 100644 --- a/pmd/src/net/sourceforge/pmd/symboltable/NameOccurrence.java +++ b/pmd/src/net/sourceforge/pmd/symboltable/NameOccurrence.java @@ -151,6 +151,11 @@ public class NameOccurrence { l = node; continue; } + + // catch this.i++ or ++this.i + if (gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression) { + return true; + } return false; }