Menu

#1175 false positive for StringBuilder.append called 2 consecutive times

PMD-5.1.1
closed
None
PMD
4-Minor
Bug
5.1.0
2014-10-24
2014-03-28
No

This was in 5.0.5 too, and is also in the 5.1.0 release. Here's how to reproduce it (change the path to the pmd-bin release)

mkdir /tmp/pmd-test
cd /tmp/pmd-test
cat >Escape.java<<EOF
public class Escape {

public static String escape(final String value)
{
    StringBuilder s = new StringBuilder(value.length() + 2);
    s.append('"');
    for (int i = 0; i < value.length(); i++) {
        s.append(value.charAt(i));
    }
    s.append('"');
    return s.toString();
}

public static void main(final String[] args) {
    System.out.println(escape(args[0]));
}
}
EOF
~/tools/pmd-bin-5.1.0/bin/run.sh pmd -R java-strings -d .
/tmp/pmd-test/Escape.java:5:    StringBuffer (or StringBuilder).append is called 2 consecutive times with literal Strings. Use a single append with a single combined String.

I can't see where there are 2 literal String appends. Possibly PMD gets tripped up by the for loop in the middle? Or maybe the '"' character is causing problems.

Discussion

  • Andreas Dangel

    Andreas Dangel - 2014-03-29
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-next
     
  • Andreas Dangel

    Andreas Dangel - 2014-03-29

    This will be fixed with the next release.

     
  • Richard Quirk

    Richard Quirk - 2014-03-30

    That was quick, thanks :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.