Skip to content

[java] StringBuilder/Buffer false negatives with typeres #2881

@adangel

Description

@adangel

Affects PMD Version: 6.30.0-SNAPSHOT

Rule: InsufficientStringBufferDeclaration,
ConsecutiveLiteralAppends,
AppendCharacterWithChar

Description:

From #2871 (comment)

Code Sample demonstrating the issue:

public class Foo {
    public void a() {
        StringBuilder sb = new StringBuilder();
        int start = sb.indexOf(" ");
        if (start == -1) {
            sb.append("Bar");
            sb.append(" "); // warning expected: Avoid appending characters as strings in StringBuffer.append. 
        }
    }
    public void b() {
        StringBuffer sb = new StringBuffer(); // warning expected: StringBuffer constructor is initialized with size 16, but has at least 17 characters appended. 
        sb.append("test "); // warning expected: StringBuffer (or StringBuilder).append is called 3 consecutive times with literals. Use a single append with a single combined String. 
        sb.append("test2 ").append("test3 ");
    }
}

Expected outcome:

  • Is PMD missing to report a violation, where there should be one? -> false-negative

Running PMD through: regression-tester

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of codegood first issueA great starting point for new contributorshelp-wanted

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions