Skip to content

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

@strkkk

Description

@strkkk

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

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions