False positive StringBuffer constructor with ?: int value
A source code analyzer
Brought to you by:
adangel,
juansotuyo
Using the strings java ruleset, the example below gives this warnings:
test.java:7: StringBuffer constructor is initialized with size 16, but has at least 42 characters appended.
However the initialization size is known to always be large enough. In PMD 5.3.3 this was correctly handled, the issue first appeared in 5.3.4.
Here's the code:
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);
}
}
As a workaround I can pull out the int value into its own variable then the warning goes away, i.e. this code is does not raise a warning:
int size = args.length > 1 ? 100 : 200;
StringBuilder report = new StringBuilder(size);
Thanks!
This will be fixed with PMD 5.3.5.
Commit: https://github.com/pmd/pmd/commit/98278cfdfcd1fd3ec855578b1f739698223f1844