#1413 False positive StringBuffer constructor with ?: int value

This commit is contained in:
Andreas Dangel
2015-09-26 11:08:21 +02:00
parent 145780de9c
commit 98278cfdfc
3 changed files with 39 additions and 5 deletions

View File

@ -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<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class);
List<ASTLiteral> 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<ASTName> 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) {

View File

@ -1036,6 +1036,26 @@ public class NullPointer {
StringBuilder builder;
builder = new StringBuilder();
}
}
]]></code>
</test-code>
<test-code>
<description>#1413 False positive StringBuffer constructor with ?: int value</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public final class test {
private test() {}
public static void main(final String ... args) {
final String NEWLINE = "\n";
StringBuilder report = new StringBuilder(args.length > 1 ? 100 : 200);
report.append(
"### Testing report" + NEWLINE +
"# Testing" + NEWLINE +
"# More Contents" + NEWLINE);
}
}
]]></code>
</test-code>

View File

@ -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:**