Menu

#1134 UseStringBufferLength: false positives

PMD-5.1.0
closed
None
PMD
3-Major
Bug
5.0.5
2014-08-26
2013-09-19
Philip Graf
No

All of the following code snippets trigger a false positive for the rule UseStringBufferLength.


equals with empty + non-empty string:

  StringBuffer sb = new StringBuffer();
  return sb.toString().equals("" + "x");

trimmed string equals "":

  StringBuffer sb = new StringBuffer(" ");
  return sb.toString().trim().equals("");

length of trimmed string == 0:

  StringBuffer sb = new StringBuffer();
  return sb.toString().trim().length == 0;

equals with "" as argument of a method invocation:

 boolean bar() {
  StringBuffer sb = new StringBuffer();
  return sb.toString().equals(baz(""));
 }
 String baz(String s) {
  return s + "x";
 }

The code of the first three snippets is far from perfect but it should not trigger UseStringBufferLength as the snippets cannot be improved by using StringBuffer.length().

Discussion

  • Philip Graf

    Philip Graf - 2013-09-19

    Here's the pull request containing the fix for this bug: https://github.com/pmd/pmd/pull/23

     
  • Andreas Dangel

    Andreas Dangel - 2013-10-09
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.1.0
     

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.