1634078 - False positive in StringToString

StringToString rule wasn't taking arrays into account.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4944 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-01-13 03:55:30 +00:00
parent 28e81e38e1
commit f308f8c4cc
3 changed files with 23 additions and 2 deletions

View File

@ -4,6 +4,7 @@ Fixed bug 1626232 - Commons logging rules (ProperLogger and UseCorrectExceptionL
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.
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

@ -79,6 +79,20 @@ public class Foo {
String[] foo = {"hi"};
return foo[0].toString();
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
ToString on String Array Object
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public String getFoo() {
String foo[] = new String[0];
foo.toString();
}
}
]]></code>
</test-code>

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.rules.strings;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.symboltable.NameOccurrence;
@ -15,10 +16,15 @@ public class StringToStringRule extends AbstractRule {
if (!node.getNameDeclaration().getTypeImage().equals("String")) {
return data;
}
boolean isArray = node.isArray();
for (Iterator i = node.getUsages().iterator(); i.hasNext();) {
NameOccurrence occ = (NameOccurrence) i.next();
if (occ.getNameForWhichThisIsAQualifier() != null && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("toString") != -1) {
addViolation(data, occ.getLocation());
if (occ.getNameForWhichThisIsAQualifier() != null) {
if (!isArray && occ.getNameForWhichThisIsAQualifier().getImage().indexOf("toString") != -1) {
addViolation(data, occ.getLocation());
} else if (isArray && occ.getNameForWhichThisIsAQualifier().getLocation() != null && !ASTName.class.equals(occ.getNameForWhichThisIsAQualifier().getLocation().getClass())) {
addViolation(data, occ.getLocation());
}
}
}
return data;