From 94cfdb93fd76d52282dab8ba24b67b6231d2de7c Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Thu, 28 Aug 2008 00:16:23 +0000 Subject: [PATCH] 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 --- pmd/etc/changelog.txt | 1 + .../strings/xml/UselessStringValueOf.xml | 15 ++++++++++- .../rules/strings/UselessStringValueOf.java | 25 +++++++++++-------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 30f639ff6a..1b217185c6 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/UselessStringValueOf.xml b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/UselessStringValueOf.xml index 3cdeda93d6..fc39879bfe 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/UselessStringValueOf.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/UselessStringValueOf.xml @@ -67,4 +67,17 @@ public static String bar17(int n) { } ]]> - \ No newline at end of file + + + 0 + + + diff --git a/pmd/src/net/sourceforge/pmd/rules/strings/UselessStringValueOf.java b/pmd/src/net/sourceforge/pmd/rules/strings/UselessStringValueOf.java index b648ff4ccf..135e29993c 100644 --- a/pmd/src/net/sourceforge/pmd/rules/strings/UselessStringValueOf.java +++ b/pmd/src/net/sourceforge/pmd/rules/strings/UselessStringValueOf.java @@ -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,16 +48,20 @@ 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 (name.getNameDeclaration() instanceof VariableNameDeclaration) { - VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration(); - if (nd.isPrimitiveType()) { - result = true; + 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(); } } }