Skip to content

[java] False positive about the rule UseStringBufferForStringAppends #4078

@llm4sast

Description

@llm4sast

Affects PMD Version:6.47.0

Rule:UseStringBufferForStringAppends

Please provide the rule name and a link to the rule documentation:
https://pmd.sourceforge.io/pmd-6.48.0/pmd_rules_java_performance.html#usestringbufferforstringappends

Description:
Hi, we are doing research in testing static analyzer. Our approach found a false positive about the rule UseStringBufferForStringAppends, PMD should report two duplicated warnings at line 5, but it is equivalent to line 3. Considering the results should be consistent, hence, I think this is a false positive.

Code Sample demonstrating the issue:

public void bar(boolean tag) {
  String x = "foo";
  x += "bar" + x;  // report one warning in this line
  if (false) {
    x += "bar" + x;  // report two duplicated warnings
  }
}

The following case is more neat, but still has similar problems:

public void bar(boolean tag) {
  String x = "foo";
  x += "bar" + x;  // report one warning in this line
  x += "bar" + x;  // report two duplicated warnings
}

Running PMD through: [Maven]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions