GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions · Issue #3152 · pmd/pmd

[java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions #3152

New issue
Copy link
New issue
Copy link
Closed
#3154
Closed
[java] ConsecutiveLiteralAppends and InsufficientStringBufferDeclaration: FP with switch expressions#3152
#3154
Copy link
Assignees
adangel
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.33.0

Description

@adangel
adangel
opened on Mar 4, 2021
Issue body actions

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("&lt;"); // 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("&gt;");
                case '"' -> builder.append("&quot;");
                case '&' -> builder.append("&amp;");
                case '\'' -> builder.append("&#39;");
                case '/' -> builder.append("&#47;");
                default -> builder.append(ch);
            }
        }
        return builder.toString();
    }
    
    private static int findHTMLReservedChar(String text) {
        return 0;
    }
}

Originally posted by @neowu in #2427 (comment)

Activity

  1. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Mar 4, 2021
  2. adangel
    self-assigned this
    on Mar 4, 2021
  3. adangel
    added this to the 6.33.0 milestone on Mar 4, 2021
  4. adangel
    added a commit that references this issue on Mar 4, 2021

    [java] ConsecutiveLiteralAppends and

    This issue will close once commit ca89cba is merged into the 'main' branch.
    ca89cba
  5. oowekyala
    closed this as completedin ca89cbaon Mar 10, 2021
  6. oowekyala
    closed this as completedin #3154on Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @adangel
    adangel

Labels

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

Type

No type

Projects

No projects

    Milestone

    • 6.33.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.