Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding equals() and not hashCode() #157

Closed
cushon opened this issue Oct 31, 2014 · 1 comment
Closed

Overriding equals() and not hashCode() #157

cushon opened this issue Oct 31, 2014 · 1 comment

Comments

@cushon
Copy link
Collaborator

cushon commented Oct 31, 2014

Original issue created by maekawa@google.com on 2013-07-03 at 04:51 AM


Describe the bug pattern this checker should detect and why it is
incorrect.

Note our criteria for new checks:
https://code.google.com/p/error-prone/wiki/NewCheckCriteria

If possible, please attach source files that contain code that should and
should not trigger the error. Code that should trigger the error should
be in a file called PositiveCases.java, and code that should not trigger
the error should be in a file called NegativeCases.java.

Otherwise, just include positive and negative cases below.

When we override equals(), we should almost always override hashCode() too.

// Positive
class Foo {
  int a;

  @Override
  public boolean equals(Object o) {
    if (o instanceof Foo) {
      return a == ((Foo) o).a;
    }
    return false;
  }
}

// Negative
class Bar {
  int a;

  @Override
  public boolean equals(Object o) {
    if (o instanceof Bar) {
      return a == ((Bar) o).a;
    }
    return false;
  }

  @Override
  public int hashCode() {
    return a;
  }
}

@cushon
Copy link
Collaborator Author

cushon commented Oct 31, 2014

Original comment posted by aeagle22206 on 2013-08-12 at 09:09 PM


(No comment entered for this change.)


Status: Duplicate
Merged Into: #129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant