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] false-positive for SimplifyStartsWith if string is empty · Issue #1868 · pmd/pmd

[java] false-positive for SimplifyStartsWith if string is empty #1868

New issue
Copy link
New issue
Copy link
Closed
#2723
Closed
[java] false-positive for SimplifyStartsWith if string is empty#1868
#2723
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematicin:documentationAffects the documentation [doc]
Milestone
6.27.0

Description

@strkkk
strkkk
opened on Jun 14, 2019
Issue body actions

Affects PMD Version: 6.14.0

Rule: SimplifyStartsWith

Description: pmd reports that expression can be simplified, but if string is empty, "simplified" code produces StringIndexOutOfBoundsException

Code Sample demonstrating the issue:

public class Example {
    public static void main(String[] args) {
        System.out.println(checkStringStartsFrom(""));
        System.out.println(checkStringCharAt(""));
    }

    private static boolean checkStringStartsFrom(String string) {
        return string.startsWith("*");
    }

    private static boolean checkStringCharAt(String string) {
        return string.charAt(0) == '*';
    }
}

Running PMD through: Maven

Activity

  1. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jun 14, 2019
  2. adangel commented on Jun 14, 2019

    @adangel
    adangel
    on Jun 14, 2019
    Member
    More actions

    True, you'd need to always check the length additionally:

    !string.isEmpty() && string.charAt(0) == '*'.

    At the very least, we should update the documentation.
    But I'm wondering, whether this performance improvement is still valid with today's VMs...

  3. adangel
    added
    in:documentationAffects the documentation [doc]
    on Jun 14, 2019
  4. jsotuyod commented on Jun 17, 2019

    @jsotuyod
    jsotuyod
    on Jun 17, 2019
    Member
    More actions

    We can probably write a JMH test to make sure of it before we make a decision, as we did for ConsecutiveAppendShouldReuse https://stackoverflow.com/questions/37672785/how-can-i-improve-performance-if-append-is-called-on-stringbuffer-or-stringb/41839223#41839223

  5. oowekyala
    closed this as completedin c161e57on Aug 25, 2020
  6. oowekyala commented on Aug 25, 2020

    @oowekyala
    oowekyala
    on Aug 25, 2020
    Member
    More actions

    The documentation issue is fixed IMO. I opened #2740 to track the evaluation of the optimization through a benchmark

  7. adangel
    added this to the 6.27.0 milestone on Aug 31, 2020
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 problematicin:documentationAffects the documentation [doc]

    Type

    No type

    Projects

    No projects

      Milestone

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