UseStringBufferLength: false positives
A source code analyzer
Brought to you by:
adangel,
juansotuyo
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().
Here's the pull request containing the fix for this bug: https://github.com/pmd/pmd/pull/23