Bug 38751 - Optionally show compiler warning when equals() is overriden but hashCode() is not
Summary: Optionally show compiler warning when equals() is overriden but hashCode() is...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 3.5 M3   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 114791 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-06-11 01:21 EDT by eclipse CLA
Modified: 2008-10-28 10:22 EDT (History)
6 users (show)

See Also:


Attachments
Proposed patch with testcases (20.70 KB, patch)
2008-10-10 12:37 EDT, Kent Johnson CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description eclipse CLA 2003-06-11 01:21:34 EDT
It is generally necessary to override hashCode() when equals() has been 
overriden.  Flagging the failure to do so will catch a fairly common 
programming mistake.
Comment 1 Philipe Mulet CLA 2003-06-11 04:49:50 EDT
Good suggestion. Will queue it for later
Comment 2 Philipe Mulet CLA 2003-06-11 04:50:33 EDT
Defer
Comment 3 Adam Kiezun CLA 2004-03-04 12:15:34 EST
+1 here
Comment 4 Olivier Thomann CLA 2006-10-06 16:58:08 EDT
*** Bug 114791 has been marked as a duplicate of this bug. ***
Comment 5 Philipe Mulet CLA 2008-10-06 13:03:04 EDT
reconsidering
Comment 6 Kent Johnson CLA 2008-10-09 12:11:43 EDT
Should we warn against the type B in this case?

class A extends Object {
  boolean equals(Object o) { return true; }
  int hashcode() { return 1; }
}
class B extends A {
  boolean equals(Object o) { return o == this; }
}


If B inherits an implemenation of hashcode() from its superclass & not Object, is that ok?
Comment 7 Philipe Mulet CLA 2008-10-09 12:27:04 EDT
I think we should only complain when inheriting Object#hashCode() (pls note the capital C). So in your testcase, I would expect no warning.
Comment 8 Kent Johnson CLA 2008-10-10 12:37:29 EDT
Created attachment 114818 [details]
Proposed patch with testcases

Philippe, please review

Its takes way to many changes to add a new option
Comment 9 Kent Johnson CLA 2008-10-10 13:22:37 EDT
Fix and tests released for 3.5M3

Opened bug against JDT/UI to add new option, see bug 250472.


Entry in build notes:

Added a new compiler warning to report a missing hashCode() method when overriding the equals() method from Object. 
This diagnosis is controlled by option: 
JavaCore.COMPILER_PB_MISSING_HASHCODE_METHOD and produces a problem marker which ID is
IProblem.MissingHashCodeMethod problem ID.

  Compiler option ID: Reporting Missing HashCode Method.
  When enabled, the compiler will issue an error or a warning if a type
  overrides Object.equals(Object) but does not override hashCode().
  Option id: "org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod"
  Possible values: { "error", "warning", "ignore" }
  Default: "ignore"
Comment 10 David Audel CLA 2008-10-28 10:22:51 EDT
Verified for 3.5M3 using I20081026-2000 build.