GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #5837: New Rule: OverrideBothEqualsAndHashCodeOnComparable by Pankraz76 · Pull Request #5856 · pmd/pmd

Conversation

@Pankraz76

@Pankraz76 Pankraz76 commented Jun 26, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fix #5837: [java] NoEmptyUUID

Related issues

  • Fix [java] New Rule: OverrideBothEqualsAndHashCodeOnComparable #5837

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Pankraz76
Pankraz76 marked this pull request as ready for review June 26, 2025 19:58
@Pankraz76

This comment was marked as resolved.

Sign in to view
@Pankraz76

Pankraz76 commented Jun 26, 2025

Copy link
Copy Markdown
Contributor Author

should i do so? @adangel

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from 0b9eb17 to 7dd4b0b Compare June 28, 2025 15:01
@Pankraz76

Pankraz76 commented Jun 28, 2025

Copy link
Copy Markdown
Contributor Author

fixed impl, add new case, rerun dogfood.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from 7dd4b0b to 6e0acde Compare June 28, 2025 15:05
adangel
adangel requested changes Jun 29, 2025
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and sorry for the late response.

I think, we should not change the existing rule OverrideBothEqualsAndHashcode. Instead, we should create a new rule.
Reasons: While this could be considered a "false negative fix", it clearly is an incompatible change (as you have notived in the dogfood runs). That's why I'd implement this as a new rule, to not break everybody's build when they upgrade PMD. If it is a new rule, end-users can decide, whether to enable the new rule or not (otherwise they won't have a choice). I only mentioned OverrideBothEqualsAndHashcode to point out, it is similar and the new rule rule be part of error prone (and not best practices).
Also: #5770 #5837 proposes a new rule and not a false-negative fix.

Having this as a new rule also allows to explain in detail, what this rule detects and why it is error prone not to override equals if Comparable is implemented - and also explain, when it is ok (e.g. if you know that you don't use any sorted collections).

Then, you can separate the cleanup of PMD's code itself into a separate PR, when we enable this rule in our ruleset.

Also, please fix PR description, as this PR mentions the wrong issue....

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Pankraz76

Pankraz76 commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Agree.

Also, my assumption was wrong — these could collide, meaning fixing one, then might collide with the other, since they are different, as supposed to.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76 Pankraz76 changed the title Fix #5770: [java] MissingEqualsOnComparable: 'Comparable' implemented but 'equals()' not overridden Fix #5837: [java] MissingEqualsOnComparable: 'Comparable' implemented but 'equals()' not overridden Jun 29, 2025
@Pankraz76
Pankraz76 marked this pull request as draft June 29, 2025 11:15
@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from 2e537d4 to a2a347f Compare June 29, 2025 11:35
@Pankraz76

Pankraz76 commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

item:

[INFO]
[INFO] --- checkstyle:3.6.0:check (checkstyle-check) @ pmd-core ---
[INFO] There is 1 error reported by Checkstyle 10.25.0 with /net/sourceforge/pmd/pmd-checkstyle-config.xml ruleset.
[ERROR] src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrToken.java:[10,1] (imports) ImportOrder: Wrong order for 'java.util.Objects' import.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for PMD 7.15.0-SNAPSHOT:
[INFO]
[INFO] PMD ................................................ SUCCESS [ 3.464

blocked by:

  • fix and automate importOrder with Maven Spotless Plugin #5776
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76 Pankraz76 mentioned this pull request Jun 29, 2025
fix and automate importOrder with Maven Spotless Plugin #5776
Closed
@Pankraz76

Pankraz76 commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

its done but blocked by the checkstyle annoyance foo.

If you consider this contribution fix the import, as im not going to. Its already fixed in fixed in purposed PR. I have invested enough time on silly imports.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 requested a review from adangel June 29, 2025 11:46
@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from a2a347f to 1882f7a Compare June 29, 2025 11:47
@github-actions

github-actions Bot commented Jun 29, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 154 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.

Regression Tester Report

(comment created at 2025-08-01 16:57:22+00:00 for 980137a)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zbynek
zbynek reviewed Jul 9, 2025
View reviewed changes
Comment thread ...c/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/MissingEqualsOnComparableRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel reviewed Jul 10, 2025
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from e6f17d1 to 54c5b1b Compare July 10, 2025 16:57
Pankraz76
Pankraz76 commented Jul 10, 2025
View reviewed changes

@Pankraz76 Pankraz76 left a comment

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convention over configuration.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from 54c5b1b to aabd960 Compare July 10, 2025 17:03
@Pankraz76
Pankraz76 marked this pull request as ready for review July 10, 2025 17:04
@Pankraz76
Pankraz76 requested review from adangel and zbynek July 10, 2025 17:05
Pankraz76
Pankraz76 commented Jul 10, 2025
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

zbynek
zbynek reviewed Jul 10, 2025
View reviewed changes
Comment thread ...c/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/MissingEqualsOnComparableRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 force-pushed the OverrideBothEqualsAndHashcode-pr branch from aabd960 to 18dec17 Compare July 11, 2025 09:39
Pankraz76
Pankraz76 commented Jul 11, 2025
View reviewed changes
Comment thread ...st/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/MissingEqualsOnComparable.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76
Pankraz76 requested a review from zbynek July 11, 2025 09:41
adangel added 2 commits August 1, 2025 11:50
@adangel
[java] OverrideBothEqualsAndHashcode - improve rule documentation
e315994
@adangel
[doc] Update release notes (pmd#5837, pmd#5856)
121f1bf
@adangel
adangel force-pushed the OverrideBothEqualsAndHashcode-pr branch from e0563a2 to 121f1bf Compare August 1, 2025 10:14
@adangel

adangel commented Aug 1, 2025

Copy link
Copy Markdown
Member

Ok, I've updated the PR now.

I explicitly did not do this:

Long term, it would make sense to merge these two rules, maybe make it configurable with a boolean property "skipComparable".
The default value of the property could be changed with PMD 8 to automatically include Comparable (and not skipping them anymore).

That should be really a separate issue/PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added this to the 7.17.0 milestone Aug 1, 2025
@adangel
[java] OverrideBothEqualsAndHashCodeOnComparable - skip enums
a274e3f
Pankraz76
Pankraz76 commented Aug 1, 2025
View reviewed changes
Comment thread ...sourceforge/pmd/lang/java/rule/errorprone/OverrideBothEqualsAndHashCodeOnComparableRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Pankraz76 and others added 4 commits August 1, 2025 13:59
@Pankraz76
[doc] Update @author Tag
d5a959f
@adangel
[java] OverrideBothEqualsAndHashCodeOnComparable - skip inherited
b99908f
Do not report, if compareTo is not overridden locally, but only
inherited.
@adangel
[java] OverrideBothEqualsAndHashCodeOnComparable - fix line number
0bdaaee
Now that we only report on types, that have a compareTo() method,
we should report on this method rather than on the entire class
when equals and hashCode are missing.
@adangel
[java] OverrideBothEqualsAndHashCodeOnComparable - improve example
ab4775a
Update code example for correct equals() implementation that
uses compareTo() and is therefore automatically consistent.
adangel
adangel reviewed Aug 1, 2025
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel reviewed Aug 1, 2025
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel
Fix example code
d298f93
adangel
adangel approved these changes Aug 1, 2025
View reviewed changes
zbynek
zbynek reviewed Aug 1, 2025
View reviewed changes
Comment thread ...sourceforge/pmd/lang/java/rule/errorprone/OverrideBothEqualsAndHashCodeOnComparableRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

zbynek
zbynek approved these changes Aug 1, 2025
View reviewed changes

@zbynek zbynek left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a comma after the when clause in the message, otherwise looks good.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
adangel and others added 2 commits August 1, 2025 18:24
@adangel @zbynek
Update pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/erro…
4a2191d
…rprone/OverrideBothEqualsAndHashCodeOnComparableRule.java

Co-authored-by: Zbynek Konecny <[email protected]>
@adangel
Fix tests
980137a
@adangel
adangel merged commit 21f4c8d into pmd:main Aug 7, 2025
@Pankraz76 Pankraz76 mentioned this pull request Aug 9, 2025
OverrideEqualsAndHashCodeOnComparableRecipe openrewrite/rewrite-static-analysis#705
Closed
@Pankraz76

Pankraz76 commented Aug 9, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

consider (potential) auto fix follow up:

  • OverrideEqualsAndHashCodeOnComparableRecipe openrewrite/rewrite-static-analysis#705
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Aug 23, 2025
[java] New rule: NoEmptyUUID #6005
Closed
Fix #6005: [java] New Rule NoEmptyUUID #6007
Closed
@Pankraz76

Pankraz76 commented Sep 1, 2025

Copy link
Copy Markdown
Contributor Author

should just have named it like InconsistentComparable, CorruptComparableContract.

This name is really long and kind of collides with best practise having the rule: big scope, small name. Which we have failed kind of. This reads like a private method name giving full detail, making the impl. obsolete to check.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76

Pankraz76 commented Sep 1, 2025

Copy link
Copy Markdown
Contributor Author

can not use it yet, as 17 released not in core, right?

https://mvnrepository.com/artifact/net.sourceforge.pmd/pmd-core/7.16.0

when is this going to happen please?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Pankraz76 Pankraz76 mentioned this pull request Sep 1, 2025
S1124: Modifiers should be declared in the correct order #6019
Closed
@adangel adangel changed the title Fix #5837: [java] New Rule OverrideBothEqualsAndHashCodeOnComparable [java] Fix #5837: New Rule OverrideBothEqualsAndHashCodeOnComparable Sep 8, 2025
@adangel adangel changed the title [java] Fix #5837: New Rule OverrideBothEqualsAndHashCodeOnComparable [java] Fix #5837: New Rule: OverrideBothEqualsAndHashCodeOnComparable Sep 12, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Jun 2, 2026
Update copied files plume-lib/bcel-util#519
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zbynek zbynek zbynek approved these changes
@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Milestone

7.17.0

Development

Successfully merging this pull request may close these issues.

[java] New Rule: OverrideBothEqualsAndHashCodeOnComparable

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Pankraz76 @adangel @zbynek
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.