Fixed bug 1977438 - False positive for UselessStringValueOf

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6414 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2008-08-28 00:16:23 +00:00
parent 91ad44ba0a
commit 94cfdb93fd
3 changed files with 30 additions and 11 deletions

View File

@ -11,6 +11,7 @@ Fixed bug 1977230 - false positive: UselessOverridingMethod
Fixed bug 1998185 - BeanMembersShouldSerialize vs @SuppressWarnings("serial")
Fixed bug 2002722 - false + in UseStringBufferForStringAppends
Fixed bug 2056318 - False positive for AvoidInstantiatingObjectsInLoops
Fixed bug 1977438 - False positive for UselessStringValueOf
Optimizations and false positive fixes in PreserveStackTrace
@SuppressWarnings("all") disables all warnings
All comment types are now stored in ASTCompilationUnit, not just formal ones

View File

@ -64,6 +64,19 @@ public class Foo {
public static String bar17(int n) {
return n + String.valueOf(n) + "n"; // no
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
[ 1977438 ] False positive for UselessStringValueOf
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
private String getMessage(Object pPassedValue) {
return "someString" + (pPassedValue == null ? "null" : '\'' + String.valueOf(pPassedValue) + '\'') + "end of my string";
}
}
]]></code>
</test-code>

View File

@ -2,6 +2,7 @@ package net.sourceforge.pmd.rules.strings;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTAdditiveExpression;
import net.sourceforge.pmd.ast.ASTLiteral;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
@ -47,18 +48,22 @@ public class UselessStringValueOf extends AbstractRule {
private static boolean isPrimitive(Node parent) {
boolean result = false;
if (parent instanceof ASTPrimaryExpression &&
parent.jjtGetNumChildren() == 1 &&
parent.jjtGetChild(0) instanceof ASTPrimaryPrefix &&
parent.jjtGetChild(0).jjtGetNumChildren() == 1 &&
parent.jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
ASTName name = (ASTName) parent.jjtGetChild(0).jjtGetChild(0);
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
Node child = parent.jjtGetChild(0);
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
Node gc = child.jjtGetChild(0);
if (gc instanceof ASTName) {
ASTName name = (ASTName) gc;
if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration();
if (nd.isPrimitiveType()) {
result = true;
}
}
} else if (gc instanceof ASTLiteral) {
result = !((ASTLiteral) gc).isStringLiteral();
}
}
}
return result;
}