From 98278cfdfcd1fd3ec855578b1f739698223f1844 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 26 Sep 2015 11:08:21 +0200 Subject: [PATCH] #1413 False positive StringBuffer constructor with ?: int value --- ...sufficientStringBufferDeclarationRule.java | 14 ++++++++++++- .../InsufficientStringBufferDeclaration.xml | 20 +++++++++++++++++++ src/site/markdown/overview/changelog.md | 10 ++++++---- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java index 4c3f75cec3..5d977dd954 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/strings/InsufficientStringBufferDeclarationRule.java @@ -3,6 +3,7 @@ */ package net.sourceforge.pmd.lang.java.rule.strings; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -11,6 +12,7 @@ import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression; +import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement; import net.sourceforge.pmd.lang.java.ast.ASTCastExpression; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; @@ -242,7 +244,15 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule { return DEFAULT_BUFFER_SIZE; } - List literals = block.findDescendantsOfType(ASTLiteral.class); + List literals; + ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class); + if (constructorCall != null) { + // if this is a constructor call, only consider the literals within it. + literals = constructorCall.findDescendantsOfType(ASTLiteral.class); + } else { + // otherwise it might be a setLength call... + literals = block.findDescendantsOfType(ASTLiteral.class); + } if (literals.isEmpty()) { List name = block.findDescendantsOfType(ASTName.class); if (!name.isEmpty()) { @@ -264,6 +274,8 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule { } else { iConstructorLength = Integer.parseInt(str); } + } else { + iConstructorLength = -1; } if (iConstructorLength == 0) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml index cbfbbab0d9..2d452c8969 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/strings/xml/InsufficientStringBufferDeclaration.xml @@ -1036,6 +1036,26 @@ public class NullPointer { StringBuilder builder; builder = new StringBuilder(); } +} + ]]> + + + + #1413 False positive StringBuffer constructor with ?: int value + 0 + 1 ? 100 : 200); + report.append( + "### Testing report" + NEWLINE + + "# Testing" + NEWLINE + + "# More Contents" + NEWLINE); + } } ]]> diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 4b8f86978c..830c40c13c 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -21,13 +21,15 @@ **Bugfixes:** * java-controversial/DefaultPackage: - [#1410](https://sourceforge.net/p/pmd/bugs/1410/): DefaultPackage triggers on field annotated with @VisibleForTesting + * [#1410](https://sourceforge.net/p/pmd/bugs/1410/): DefaultPackage triggers on field annotated with @VisibleForTesting * java-design/CloseResource: - [#1387](https://sourceforge.net/p/pmd/bugs/1387/): CloseResource has false positive for ResultSet + * [#1387](https://sourceforge.net/p/pmd/bugs/1387/): CloseResource has false positive for ResultSet * java-strings/InsufficientStringBufferDeclaration: - [#1409](https://sourceforge.net/p/pmd/bugs/1409/): NullPointerException in InsufficientStringBufferRule + * [#1409](https://sourceforge.net/p/pmd/bugs/1409/): NullPointerException in InsufficientStringBufferRule + * [#1413](https://sourceforge.net/p/pmd/bugs/1413/): False positive StringBuffer constructor with ?: int value * java-unnecessary/UselessParentheses: - [#1407](https://sourceforge.net/p/pmd/bugs/1407/): UselessParentheses "&" and "+" operator precedence + * [#1407](https://sourceforge.net/p/pmd/bugs/1407/): UselessParentheses "&" and "+" operator precedence + **API Changes:**