Skip to content

[java] Improve message of InefficientEmptyStringCheck for String.trim().isEmpty() #1125

@krichter722

Description

@krichter722

Affects PMD Version: 6.3.0

Rule: InefficientEmptyStringCheck

Description:

public boolean method0(String arg0) {
    return arg0.trim().isEmpty();
}

produces the message InefficientEmptyStringCheck Priority:3 String.trim().length()==0 is an inefficient way to validate an empty String.. which caused the following misunderstanding on my side: I somehow assumed that there's a difference between String.length()==0 and String.isEmpty() like there is for Collection.size()==0 and Collection.isEmpty() and PMD is confused by the trim in between because of the message. That's not the case and only learned afterwards the String.trim.[length check] is inefficient. It's a rare case, but I'd like to share it together with the suggestion to make the error message say exactly what's written in the code in order to avoid it for anyone else.

Code Sample demonstrating the issue:

see above

Running PMD through: Maven (Plugin version 3.9.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rulesin:documentationAffects the documentation [doc]

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions