You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] OnlyOneReturn: False positive with equals method #4257
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:
@Overridepublicbooleanequals(Objecto) { // a case generated by the lombok annotationif (o == this) returntrue;
if (!(oinstanceofEqualsAndHashCodeExample)) returnfalse;
EqualsAndHashCodeExampleother = (EqualsAndHashCodeExample) o;
if (!other.canEqual((Object)this)) returnfalse;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) returnfalse;
if (Double.compare(this.score, other.score) != 0) returnfalse;
if (!Arrays.deepEquals(this.tags, other.tags)) returnfalse;
returntrue;
}
Expected outcome:
PMD reports a violation at line 3, but that's wrong. That's a false positive.
changed the title [-][java] OnlyOneReturn: False positive with equals methods[/-][+][java] OnlyOneReturn: False positive with equals method[/+]on Dec 22, 2025
Activity
[-]OnlyOneReturn should not consider equals[/-][+][java] OnlyOneReturn should not consider equals[/+]adangel commentedon Jan 19, 2023
We could indeed add a property to ignore
equalsby default.The similar checkstyle rule ReturnCount ignores equals by default (see property format).
[-][java] OnlyOneReturn should not consider equals[/-][+][java] OnlyOneReturn: False positive with equals methods[/+][-][java] OnlyOneReturn: False positive with equals methods[/-][+][java] OnlyOneReturn: False positive with equals method[/+][doc] Update release notes (#4257, #6309)
[java] Fix pmd#4257: Allow ignoring methods in OnlyOneReturn (pmd#6309)
RM-37163: CPD 7.21.0 upgrade (#8)