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] InsufficientStringBufferDeclaration false positive with StringBuilder.setLength(0) · Issue #3712 · pmd/pmd

[java] InsufficientStringBufferDeclaration false positive with StringBuilder.setLength(0) #3712

New issue
Copy link
New issue
Copy link
Closed
#3745
Closed
[java] InsufficientStringBufferDeclaration false positive with StringBuilder.setLength(0)#3712
#3745
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.42.0

Description

@gredler
gredler
opened on Dec 30, 2021
Issue body actions

Affects PMD Version: 6.41.0

Rule: InsufficientStringBufferDeclaration

Description:

When using StringBuilder.setLength(0) to reset the StringBuilder for reuse (to avoid allocating additional instances, especially in loops), PMD assumes that the builder length is 0 if it cannot determine the length from the constructor invocation, and warns about a too-small builder.

There does not seem to be an issue when the constructor uses a hardcoded size, only when the initial constructor size is calculated at runtime and PMD is unable to determine the actual initial size.

Code Sample demonstrating the issue:

    public static void foo(int i) {
        StringBuilder sb = new StringBuilder(i);
        sb.append("ab");
        sb.setLength(0); // false positive
        sb.append("cd");
    }

    public static void foo2() {
        StringBuilder sb = new StringBuilder(2);
        sb.append("ab");
        sb.setLength(0); // no problem, no false positive
        sb.append("cd");
    }

Expected outcome:

PMD reports a violation at line 4, but that's wrong. If PMD cannot determine the initial length from the constructor invocation, it should not assume that the length specified in setLength( ) is the maximum length -- especially when setLength(0) is used, which is basically always a request to reset the builder, not grow it.

Running PMD through: Gradle

Activity

  1. gredler
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Dec 30, 2021
  2. gredler
    added a commit that references this issue on Jan 20, 2022

    Fix pmd#3712: InsufficientStringBufferDeclaration false positive with…

    This issue will close once commit f37a99d is merged into the 'master' branch.
    f37a99d
  3. adangel
    added this to the 6.42.0 milestone on Jan 20, 2022
  4. adangel
    added 2 commits that reference this issue on Jan 20, 2022

    [doc] Update release notes (pmd#3712, pmd#3745)

    869419c

    Merge pull request pmd#3745 from gredler:pmd-3712

    3b8f87a
  5. adangel
    closed this as completedin #3745on Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.42.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.