From f37a99d65119dee4f04ed6af40a9e2e4359da2cc Mon Sep 17 00:00:00 2001 From: Daniel Gredler Date: Wed, 19 Jan 2022 20:37:14 -0500 Subject: [PATCH] Fix #3712: InsufficientStringBufferDeclaration false positive with StringBuilder.setLength(0) --- docs/pages/release_notes.md | 1 + ...sufficientStringBufferDeclarationRule.java | 2 +- .../InsufficientStringBufferDeclaration.xml | 29 +++++++++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 2a0963f25cf..67eaa0a337b 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -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 diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java index 821cd700ebd..a2e04e79278 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InsufficientStringBufferDeclarationRule.java @@ -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; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml index c9a6913e37f..a37c1bbb175 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/InsufficientStringBufferDeclaration.xml @@ -1220,13 +1220,14 @@ public class FalsePositive { Calculated initial size in constructor - 4 - 10,34,44,61 + 5 + 10,34,44,61,88 StringBuilder has been initialized with size 4, but has at least 5 characters appended. StringBuilder has been initialized with size 5, but has at least 6 characters appended. StringBuilder has been initialized with size 8, but has at least 10 characters appended. StringBuilder has been initialized with size 8, but has at least 9 characters appended. + StringBuilder has been initialized with size 7, but has at least 8 characters appended. violation here + sb.append("aaaa"); // length is 8 + return sb.toString(); + } } ]]>