Skip to content

[java] ConsecutiveAppendsShouldReuse: False positive within if-statement without curly braces #4910

Closed

Description

@cowwoc

Affects PMD Version:
7.0.0

Rule: ConsecutiveAppendsShouldReuse

Description:
It seems that invoking StringBuilder.append() within an if-statement triggers a false-positive of the ConsecutiveAppendsShouldReuse rule. At least, I don't see how the code could be modified to please this rule.

Code Sample demonstrating the issue:

StringBuilder message = new StringBuilder();
if (nameOfOther == null)
	message.append(validator.configuration().stringMappers().toString(other));
else
	message.append(MessageBuilder.quoteName(nameOfOther));
message.append('.');

Error Message:

[INFO] PMD Failure: com.github.cowwoc.requirements.java.internal.implementation.message.ComparableMessages:47 Rule:ConsecutiveAppendsShouldReuse Priority:3 StringBuffer (or StringBuilder).append is called consecutively without reusing the target variable..

where line 47 points to message.append(validator.configuration().stringMappers().toString(other));

Running PMD through: Maven
maven-pmd-plugin 3.21.2

Activity

  1. added
    a:false-positive
    on Apr 2, 2024
  2. PimvanderLoos commented on Jun 29, 2024

    @PimvanderLoos
    Contributor

    Still happens on 7.3.0.

    Specifically, the issue seems to trigger when omitting the curly braces for both the if-clause and the else-clause.

    For example, the foo method below will trigger ConsecutiveAppendsShouldReuse. The methods bar, baz, and qux will not trigger the rule.

    public static void foo(boolean condition, StringBuilder builder) {
        if (condition)
            builder.append("foo"); // <-- ConsecutiveAppendsShouldReuse at this line
        else
            builder.append("bar");
    }
    
    public static void bar(boolean condition, StringBuilder builder) {
        if (condition) {
            builder.append("foo");
        } else {
            builder.append("bar");
        }
    }
    
    public static void baz(boolean condition, StringBuilder builder) {
        if (condition)
            builder.append("foo");
        else {
            builder.append("bar");
        }
    }
    
    public static void qux(boolean condition, StringBuilder builder) {
        if (condition) {
            builder.append("foo");
        } else
            builder.append("bar");
    }
  3. changed the title [-][java] if-statement triggers ConsecutiveAppendsShouldReuse[/-] [+][java] ConsecutiveAppendsShouldReuse: False positive within if-statement without curly brackets[/+] on Dec 24, 2025
  4. changed the title [-][java] ConsecutiveAppendsShouldReuse: False positive within if-statement without curly brackets[/-] [+][java] ConsecutiveAppendsShouldReuse: False positive within if-statement without curly braces[/+] on Dec 24, 2025
  5. added this to the 7.20.0 milestone on Dec 24, 2025
  6. added a commit that references this issue on Dec 24, 2025
  7. added a commit that references this issue on Dec 24, 2025
    cceb9c4
  8. added a commit that references this issue on Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions