Menu

#1413 False positive StringBuffer constructor with ?: int value

PMD-5.3.5
closed
None
PMD
4-Minor
Bug
5.3.4
2015-09-26
2015-09-23
No

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);

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-09-26
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.5
     

Log in to post a comment.