diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 5345b379ca..386f458043 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseStringBufferForStringAppends.xml b/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseStringBufferForStringAppends.xml index a64ece3744..cc710634b2 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseStringBufferForStringAppends.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/optimizations/xml/UseStringBufferForStringAppends.xml @@ -99,6 +99,38 @@ public class Foo{ for (int i = 0; i < 10; i++){ result = result + i; } } +} + ]]> + + + + 0 + + + + + 0 + diff --git a/pmd/src/net/sourceforge/pmd/rules/optimization/UseStringBufferForStringAppends.java b/pmd/src/net/sourceforge/pmd/rules/optimization/UseStringBufferForStringAppends.java index 0bdee7e0a4..cccec33932 100644 --- a/pmd/src/net/sourceforge/pmd/rules/optimization/UseStringBufferForStringAppends.java +++ b/pmd/src/net/sourceforge/pmd/rules/optimization/UseStringBufferForStringAppends.java @@ -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){