Skip to content

Commit

Permalink
Fix #3712: InsufficientStringBufferDeclaration false positive with St…
Browse files Browse the repository at this point in the history
…ringBuilder.setLength(0)
  • Loading branch information
gredler committed Jan 20, 2022
1 parent 1952954 commit f37a99d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/pages/release_notes.md
Expand Up @@ -46,6 +46,7 @@ This is a {{ site.pmd.release_type }} release.
* [#3701](https://github.com/pmd/pmd/issues/3701): \[java] MissingStaticMethodInNonInstantiatableClass false positive with method inner classes
* java-performance
* [#3492](https://github.com/pmd/pmd/issues/3492): \[java] UselessStringValueOf: False positive when there is no initial String to append to
* [#3712](https://github.com/pmd/pmd/issues/3712): \[java] InsufficientStringBufferDeclaration false positive with StringBuilder.setLength(0)

### API Changes

Expand Down
Expand Up @@ -97,7 +97,7 @@ public Object visit(ASTVariableDeclaratorId node, Object data) {

if (n.getImage().endsWith("setLength")) {
int newLength = getConstructorLength(n, 0);
if (newLength > constructorLength) {
if (constructorLength != -1 && newLength > constructorLength) {
constructorLength = newLength; // a bigger setLength increases capacity
rootNode = n;
}
Expand Down
Expand Up @@ -1220,13 +1220,14 @@ public class FalsePositive {

<test-code>
<description>Calculated initial size in constructor</description>
<expected-problems>4</expected-problems>
<expected-linenumbers>10,34,44,61</expected-linenumbers>
<expected-problems>5</expected-problems>
<expected-linenumbers>10,34,44,61,88</expected-linenumbers>
<expected-messages>
<message>StringBuilder has been initialized with size 4, but has at least 5 characters appended.</message>
<message>StringBuilder has been initialized with size 5, but has at least 6 characters appended.</message>
<message>StringBuilder has been initialized with size 8, but has at least 10 characters appended.</message>
<message>StringBuilder has been initialized with size 8, but has at least 9 characters appended.</message>
<message>StringBuilder has been initialized with size 7, but has at least 8 characters appended.</message>
</expected-messages>
<code><![CDATA[
public class InsufficientStringBufferDeclaration {
Expand Down Expand Up @@ -1296,6 +1297,30 @@ public class InsufficientStringBufferDeclaration {
sb.append('d'); // length is now 9
return sb.toString();
}
public String case9_sufficient_setLength() {
StringBuilder sb = new StringBuilder(4);
sb.append("xxxx");
sb.setLength(0); // length is 0, capacity is still 4
sb.append("aaaa");
return sb.toString();
}
public String case10_unknown_setLength(String in) {
StringBuilder sb = new StringBuilder(in.length()); // unknown
sb.append("xxxx");
sb.setLength(0); // length is 0, capacity is still unknown
sb.append("aaaa");
return sb.toString();
}
public String case11_unknown_ensureCapacity(String in) {
StringBuilder sb = new StringBuilder(in.length()); // unknown
sb.append("xxxx"); // length is 4
sb.ensureCapacity(7); // line 88 - length is still 4, new capacity now at least 7 -> violation here
sb.append("aaaa"); // length is 8
return sb.toString();
}
}
]]></code>
</test-code>
Expand Down

0 comments on commit f37a99d

Please sign in to comment.