forked from phoedos/pmd
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
This commit is contained in:
@ -303,4 +303,47 @@ StringBuffer buffer = new StringBuffer(
|
||||
} }
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
1503099, init with two string lengths
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar(String str1, String str2) {
|
||||
StringBuffer buf = new StringBuffer(str1.length() + str2.length());
|
||||
buf.append(str1);
|
||||
buf.append(str2);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
1503099, append with two string lengths
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar(String str1, String str2) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append(str1.length() + str2.length());
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<!--test-code>
|
||||
<description><![CDATA[
|
||||
1503099, adding two integers
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar(int a, int b) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append(a + b);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code-->
|
||||
</test-data>
|
@ -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<ASTName> 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user