From dee7967d2ac3290424a0dd1dbd1553aaaa193d02 Mon Sep 17 00:00:00 2001 From: Wouter Zelle Date: Thu, 25 Oct 2007 14:56:24 +0000 Subject: [PATCH] 1503099, fixed false positive for new StringBuffer(str1.length() + str2.length()); git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5585 51baf565-9d33-0410-a72c-fc3788e3496d --- .../xml/InefficientStringBuffering.xml | 43 +++++++++++++++++++ .../strings/InefficientStringBuffering.java | 11 ++++- 2 files changed, 53 insertions(+), 1 deletion(-) 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); }