Menu

#1557 [java] UnusedNullCheckInEquals false positive when Objects.equals is used

New Tickets
more-info-needed
None
PMD
3-Major
Bug
5.5.2
java/unnecessary/UnusedNullCheckInEquals
2017-01-08
2016-12-12
No

Latest (5.5.2) PMD complains on this code:

private static boolean isOwner(SeriesDto series) {
    Integer userId = SecurityContextUtils.getUserId();
    return userId != null
        && Objects.equals(
            series.getCreatedBy(),
            userId
        );
}

[INFO] PMD Failure: ru.mystamps.web.controller.SeriesController:481 Rule:UnusedNullCheckInEquals Priority:3 Invoke equals() on the object you've already ensured is not null.

I'm sure that there is no error because I'm checking that first value is not null AND values are equal.

Discussion

  • Andreas Dangel

    Andreas Dangel - 2017-01-08
    • summary: UnusedNullCheckInEquals false positive when Objects.equals is used --> [java] UnusedNullCheckInEquals false positive when Objects.equals is used
    • status: open --> more-info-needed
    • assigned_to: Andreas Dangel
     
  • Andreas Dangel

    Andreas Dangel - 2017-01-08

    Hi,
    thanks for reporting.
    I think, PMD is suggesting, since you already checked that userId is not null, you should use the equals method of this object:

    return userId != null && userId.equals(series.getCreatedBy())

    It seems, you can't just use Objects.equals as-is, since you don't want to allow access if both the userId and series.getCreatedBy() would be null.

    Does this make sense?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.