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:
@ -11,6 +11,7 @@ Fixed bug 1977230 - false positive: UselessOverridingMethod
|
|||||||
Fixed bug 1998185 - BeanMembersShouldSerialize vs @SuppressWarnings("serial")
|
Fixed bug 1998185 - BeanMembersShouldSerialize vs @SuppressWarnings("serial")
|
||||||
Fixed bug 2002722 - false + in UseStringBufferForStringAppends
|
Fixed bug 2002722 - false + in UseStringBufferForStringAppends
|
||||||
Fixed bug 2056318 - False positive for AvoidInstantiatingObjectsInLoops
|
Fixed bug 2056318 - False positive for AvoidInstantiatingObjectsInLoops
|
||||||
|
Fixed bug 1977438 - False positive for UselessStringValueOf
|
||||||
Optimizations and false positive fixes in PreserveStackTrace
|
Optimizations and false positive fixes in PreserveStackTrace
|
||||||
@SuppressWarnings("all") disables all warnings
|
@SuppressWarnings("all") disables all warnings
|
||||||
All comment types are now stored in ASTCompilationUnit, not just formal ones
|
All comment types are now stored in ASTCompilationUnit, not just formal ones
|
||||||
|
@ -67,4 +67,17 @@ public static String bar17(int n) {
|
|||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
</test-data>
|
<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>
|
||||||
|
</test-data>
|
||||||
|
@ -2,6 +2,7 @@ package net.sourceforge.pmd.rules.strings;
|
|||||||
|
|
||||||
import net.sourceforge.pmd.AbstractRule;
|
import net.sourceforge.pmd.AbstractRule;
|
||||||
import net.sourceforge.pmd.ast.ASTAdditiveExpression;
|
import net.sourceforge.pmd.ast.ASTAdditiveExpression;
|
||||||
|
import net.sourceforge.pmd.ast.ASTLiteral;
|
||||||
import net.sourceforge.pmd.ast.ASTName;
|
import net.sourceforge.pmd.ast.ASTName;
|
||||||
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
|
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
|
||||||
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
|
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
|
||||||
@ -47,16 +48,20 @@ public class UselessStringValueOf extends AbstractRule {
|
|||||||
|
|
||||||
private static boolean isPrimitive(Node parent) {
|
private static boolean isPrimitive(Node parent) {
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
if (parent instanceof ASTPrimaryExpression &&
|
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
|
||||||
parent.jjtGetNumChildren() == 1 &&
|
Node child = parent.jjtGetChild(0);
|
||||||
parent.jjtGetChild(0) instanceof ASTPrimaryPrefix &&
|
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
|
||||||
parent.jjtGetChild(0).jjtGetNumChildren() == 1 &&
|
Node gc = child.jjtGetChild(0);
|
||||||
parent.jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
|
if (gc instanceof ASTName) {
|
||||||
ASTName name = (ASTName) parent.jjtGetChild(0).jjtGetChild(0);
|
ASTName name = (ASTName) gc;
|
||||||
if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
|
if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
|
||||||
VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration();
|
VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration();
|
||||||
if (nd.isPrimitiveType()) {
|
if (nd.isPrimitiveType()) {
|
||||||
result = true;
|
result = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (gc instanceof ASTLiteral) {
|
||||||
|
result = !((ASTLiteral) gc).isStringLiteral();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user