Fixed bug 2002722 - false + in UseStringBufferForStringAppends

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6334 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch 2008-07-22 17:54:45 +00:00
parent d1e9279f3a
commit 169ab5d225
3 changed files with 41 additions and 1 deletions

View File

@ -7,6 +7,7 @@ Fixed bug 1988829 - Violation reported without source file name (actually a fix
Fixed bug 1989814 - false +: ConsecutiveLiteralAppends
Fixed bug 1977230 - false positive: UselessOverridingMethod
Fixed bug 1998185 - BeanMembersShouldSerialize vs @SuppressWarnings("serial")
Fixed bug 2002722 - false + in UseStringBufferForStringAppends
Optimizations and false positive fixes in PreserveStackTrace
@SuppressWarnings("all") disables all warnings

View File

@ -99,6 +99,38 @@ public class Foo{
for (int i = 0; i < 10; i++){
result = result + i;
} }
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
false positive bug #2002722
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
String dtdColumnNames[] = null;
String sortedDtdColumns[] = null;
final int loop = 0;
dtdColumnNames[loop] = dtdColumnNames[loop].trim ( );
sortedDtdColumns[loop] = sortedDtdColumns[loop].trim ( );
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
false positive bug #2002722, different bug in comment section
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar2() {
String foo ="foo";
foo = mangleTheInput(foo);
}
String mangleTheInput(final String s) { return s;}
}
]]></code>
</test-code>

View File

@ -1,6 +1,7 @@
package net.sourceforge.pmd.rules.optimization;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTArgumentList;
import net.sourceforge.pmd.ast.ASTAssignmentOperator;
import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.ast.ASTName;
@ -14,8 +15,9 @@ import net.sourceforge.pmd.typeresolution.TypeHelper;
public class UseStringBufferForStringAppends extends AbstractRule {
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class)) {
if (!TypeHelper.isA(node, String.class) || node.isArray()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent();
@ -28,6 +30,11 @@ public class UseStringBufferForStringAppends extends AbstractRule {
if (statement == null) {
continue;
}
ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in method call
continue;
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0).getClass().equals(ASTPrimaryExpression.class)) {
ASTName astName = ((SimpleNode) statement.jjtGetChild(0)).getFirstChildOfType(ASTName.class);
if(astName != null){