Skip to content

Commit

Permalink
Merge pull request pmd#3745 from gredler:pmd-3712
Browse files Browse the repository at this point in the history
[java] Fix pmd#3712: InsufficientStringBufferDeclaration setLength false
positive pmd#3745

* pr-3745:
  [doc] Update release notes (pmd#3712, pmd#3745)
  Fix pmd#3712: InsufficientStringBufferDeclaration false positive with
StringBuilder.setLength(0)
  • Loading branch information
adangel committed Jan 20, 2022
2 parents 8f7dc0f + 869419c commit 3b8f87a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -54,6 +54,7 @@ not support all features of the latest EcmaScript standard.
* [#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)
* javascript
* [#3703](https://github.com/pmd/pmd/issues/3703): \[javascript] Error - no Node adapter class registered for XmlPropRef

Expand All @@ -73,6 +74,7 @@ not support all features of the latest EcmaScript standard.
* [#3727](https://github.com/pmd/pmd/pull/3727): \[java] #3724 - fix FinalFieldCouldBeStatic: triggers only if the referenced name is static - [Oleksii Dykov](https://github.com/dykov)
* [#3742](https://github.com/pmd/pmd/pull/3742): \[java] Fix #3701 - fix MissingStaticMethodInNonInstantiatableClass for method local classes - [Oleksii Dykov](https://github.com/dykov)
* [#3744](https://github.com/pmd/pmd/pull/3744): \[core] Updated SaxonXPathRuleQueryTest.java - [Vyom Yadav](https://github.com/Vyom-Yadav)
* [#3745](https://github.com/pmd/pmd/pull/3745): \[java] Fix #3712: InsufficientStringBufferDeclaration setLength false positive - [Daniel Gredler](https://github.com/gredler)

{% endtocmaker %}

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 3b8f87a

Please sign in to comment.