diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/InefficientStringBuffering.xml b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/InefficientStringBuffering.xml index d1a3388550..37a9cb0fe3 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/InefficientStringBuffering.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/InefficientStringBuffering.xml @@ -303,4 +303,47 @@ StringBuffer buffer = new StringBuffer( } } ]]> + + + 0 + + + + + 1 + + + \ No newline at end of file diff --git a/pmd/src/net/sourceforge/pmd/rules/strings/InefficientStringBuffering.java b/pmd/src/net/sourceforge/pmd/rules/strings/InefficientStringBuffering.java index c9a68946de..274634e500 100644 --- a/pmd/src/net/sourceforge/pmd/rules/strings/InefficientStringBuffering.java +++ b/pmd/src/net/sourceforge/pmd/rules/strings/InefficientStringBuffering.java @@ -17,6 +17,7 @@ import net.sourceforge.pmd.ast.SimpleNode; import net.sourceforge.pmd.symboltable.VariableNameDeclaration; import net.sourceforge.pmd.typeresolution.TypeHelper; +import java.util.Iterator; import java.util.List; /* @@ -65,8 +66,16 @@ public class InefficientStringBuffering extends AbstractRule { } } - if (bs.isAllocation()) { + for (Iterator iterator = nameNodes.iterator(); iterator.hasNext();) { + ASTName name = iterator.next(); + if (!name.getImage().endsWith("length")) { + break; + } else if (!iterator.hasNext()) { + return data; //All names end with length + } + } + if (isAllocatedStringBuffer(node)) { addViolation(data, node); }