Fixed bug 1631646 - UselessOperationOnImmutable doesn't throw on variable.method().variable.

This defect falls under the bigger umbrella of issues around getUsages. A more global fix was eluding me - Fixing directly


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4945 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-01-14 01:44:30 +00:00
parent f308f8c4cc
commit 97faa6fe61
3 changed files with 15 additions and 2 deletions

View File

@ -5,6 +5,7 @@ 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
Fixed bug 1634078 - StringToString now recognizes toString on a String Array, rather than an element.
Fixed bug 1631646 - UselessOperationOnImmutable doesn't throw on variable.method().variable.
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

@ -20,6 +20,7 @@ public class UselessOperationOnImmutableTest extends SimpleAggregatorTst {
new TestDescriptor(TEST2, "useless operation on BigInteger", 1, rule),
new TestDescriptor(TEST3, "using the result, so OK", 0, rule),
new TestDescriptor(TEST4, "using the result in a method call, so OK", 0, rule),
new TestDescriptor(TEST6, "BigInteger obtained from compound method call", 0, rule),
});
runTests(new TestDescriptor[]{
@ -69,4 +70,13 @@ public class UselessOperationOnImmutableTest extends SimpleAggregatorTst {
" }" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST6 =
"public class Foo {" + PMD.EOL +
" public String toString() {" + PMD.EOL +
" Bar _b;" + PMD.EOL +
" java.math.BigInteger n = _b.getBigIntContainer().n;" + PMD.EOL +
" return n.toString();" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -6,9 +6,9 @@ import java.util.Set;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTExpression;
import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.util.CollectionUtil;
@ -38,7 +38,9 @@ public class UselessOperationOnImmutable extends AbstractRule {
String variableName = var.getImage();
for (Iterator it = var.getUsages().iterator(); it.hasNext();) {
NameOccurrence no = (NameOccurrence) it.next();
ASTName sn = (ASTName) no.getLocation();
// FIXME - getUsages will return everything with the same name as the variable,
// see JUnit test, case 6. Changing to SimpleNode below, revisit when getUsages is fixed
SimpleNode sn = (SimpleNode) no.getLocation();
if (!sn.jjtGetParent().jjtGetParent().jjtGetParent().getClass().equals(ASTExpression.class)) {
String methodCall = sn.getImage().substring(variableName.length());
if (targetMethods.contains(methodCall)) {