Skip to content

[java] OnlyOneReturn: False positive with equals method #4257

Closed

Description

@sarser2048

Affects PMD Version:
6.51.0+
7.0.0

Rule: OnlyOneReturn

Description:
When used PMD, I found many false positives about the rule OnlyOneReturn. I think we usually have multi return statements in equals method.

For example, the annotation EqualsAndHashCode from lombok usually generate many return statements in equals method. There are also many examples in large java projects, e.g., Spring.

Code Sample demonstrating the issue:

@Override
public boolean equals(Object o) {  // a case generated by the lombok annotation
    if (o == this) return true;
    if (!(o instanceof EqualsAndHashCodeExample)) return false;
    EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;
    if (!other.canEqual((Object)this)) return false;
    if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
    if (Double.compare(this.score, other.score) != 0) return false;
    if (!Arrays.deepEquals(this.tags, other.tags)) return false;
    return true;
  }

Expected outcome:

PMD reports a violation at line 3, but that's wrong. That's a false positive.

Running PMD through: [Maven]

Activity

  1. added
    a:false-positive
    on Dec 8, 2022
  2. changed the title [-]OnlyOneReturn should not consider equals[/-] [+][java] OnlyOneReturn should not consider equals[/+] on Jan 19, 2023
  3. adangel commented on Jan 19, 2023

    @adangel
    Member

    We could indeed add a property to ignore equals by default.
    The similar checkstyle rule ReturnCount ignores equals by default (see property format).

  4. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  5. removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  6. changed the title [-][java] OnlyOneReturn should not consider equals[/-] [+][java] OnlyOneReturn: False positive with equals methods[/+] on Dec 22, 2025
  7. changed the title [-][java] OnlyOneReturn: False positive with equals methods[/-] [+][java] OnlyOneReturn: False positive with equals method[/+] on Dec 22, 2025
  8. added this to the 7.20.0 milestone on Dec 22, 2025
  9. added a commit that references this issue on Dec 22, 2025
  10. added a commit that references this issue on Dec 22, 2025
    8eb7137
  11. added a commit that references this issue on Feb 10, 2026
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-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions