pmd: fix #976 UselessStringValueOf wrong when appending character arrays

This commit is contained in:
Andreas Dangel
2013-03-16 19:51:06 +01:00
parent 4e5ca16990
commit 040d465137
3 changed files with 20 additions and 4 deletions

View File

@ -1,5 +1,6 @@
????? ??, 2013 - 5.0.3:
Fixed bug 976: UselessStringValueOf wrong when appending character arrays
Fixed bug 977: MisplacedNullCheck makes false positives
Fixed bug 984: Cyclomatic complexity should treat constructors like methods
Fixed bug 985: Suppressed methods shouldn't affect avg CyclomaticComplexity

View File

@ -2,10 +2,13 @@ package net.sourceforge.pmd.lang.java.rule.strings;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression;
import net.sourceforge.pmd.lang.java.ast.ASTArgumentList;
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
@ -25,6 +28,20 @@ public class UselessStringValueOfRule extends AbstractJavaRule {
if (parent.jjtGetNumChildren() != 2) {
return super.visit(node, data);
}
// skip String.valueOf(anyarraytype[])
ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
ASTName arg = args.getFirstDescendantOfType(ASTName.class);
if (arg != null) {
ASTType argType = arg.getNameDeclaration().getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null
&& argType.jjtGetChild(0) instanceof ASTReferenceType
&& ((ASTReferenceType)argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
}
}
}
Node gp = parent.jjtGetParent();
if (parent instanceof ASTPrimaryExpression &&
gp instanceof ASTAdditiveExpression &&

View File

@ -80,10 +80,8 @@ public class Foo {
}
]]></code>
</test-code>
<test-code regressionTest="false">
<description><![CDATA[
[3394465] False positive for UselessStringValueOf
]]></description>
<test-code>
<description>#976 False positive for UselessStringValueOf</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
class TestClass {