Fixed bug 1531216 - ImmutableField

NameOccurence did not detect this.x++ or ++this.x as a self-assignment, even though x++ or ++x was.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4943 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-01-13 03:05:27 +00:00
parent 32f881bb60
commit 28e81e38e1
4 changed files with 67 additions and 0 deletions

View File

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

View File

@ -240,4 +240,25 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
assignment through this
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo{
private int counter;
private Foo(){
counter = 0;
}
private int foo(){
if (++this.counter < 3) {
return 0;
}
return 1;
}
}
]]></code>
</test-code>
</test-data>

View File

@ -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 +
"}";
}

View File

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