Since PMD 6.27.0, still in the latest 6.32.0, needs at least Java15 due to Switch Expressions
Rules: ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration
I encountered similar issues, with PMD 6.31.0/6.32.0
the following code are causing both PMD.InsufficientStringBufferDeclaration, PMD.ConsecutiveLiteralAppends
PMD 6.30.0 works fine
public class FalsePositive { public static String escapeHTML(String text) { int length = text.length(); int index = findHTMLReservedChar(text); if (index == length) return text; var builder = new StringBuilder(length * 2); // Rule:InsufficientStringBufferDeclaration Priority:3 StringBuffer constructor is initialized with size 16, but has at least 29 characters appended.. for (int i = 0; i < index; i++) builder.append(text.charAt(i)); for (; index < length; index++) { char ch = text.charAt(index); switch (ch) { case '<' -> builder.append("<"); // Rule:ConsecutiveLiteralAppends Priority:3 StringBuffer (or StringBuilder).append is called 6 consecutive times with literals. Use a single append with a single combined String.. case '>' -> builder.append(">"); case '"' -> builder.append("""); case '&' -> builder.append("&"); case '\'' -> builder.append("'"); case '/' -> builder.append("/"); default -> builder.append(ch); } } return builder.toString(); } private static int findHTMLReservedChar(String text) { return 0; } }
Originally posted by @neowu in #2427 (comment)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4