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

Add new Lint/IdentityComparison cop #8699

Merged
merged 1 commit into from Sep 11, 2020

Conversation

koic
Copy link
Member

@koic koic commented Sep 11, 2020

Follow rubocop/ruby-style-guide#835.

This PR adds new Lint/IdentityComparison cop.

# bad
foo.object_id == bar.object_id

# good
foo.equal?(bar)

The cop prefers equal? over == when comparing object_id.
Object#equal? is provided to compare objects for identity, and in contrast Object#== is provided for the purpose of doing value comparison.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Follow rubocop/ruby-style-guide#835.

This PR adds new `Lint/IdentityComparison` cop.

```ruby
# bad
foo.object_id == bar.object_id

# good
foo.equal?(bar)
```

The cop prefers `equal?` over `==` when comparing `object_id`.
`Object#equal?` is provided to compare objects for identity, and in contrast
`Object#==` is provided for the purpose of doing value comparison.
@bbatsov bbatsov merged commit eca7d75 into rubocop:master Sep 11, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 11, 2020

Nice! I love cops that find problems in our codebase. :-)

@marcandre
Copy link
Contributor

Super!
I think you could have used RESTRICT_ON_SEND here, though

@koic koic deleted the add_new_lint_identity_comparison_cop branch September 12, 2020 06:24
@koic
Copy link
Member Author

koic commented Sep 12, 2020

I think you could have used RESTRICT_ON_SEND here, though

Yeah, I will update this one. Thank you for the advice!

koic added a commit to koic/rubocop that referenced this pull request Sep 12, 2020
Follow rubocop#8699 (comment)
and rubocop#8606.

This PR uses `RESTRICT_ON_SEND` for `Lint/IdentityComparison`.
bbatsov pushed a commit that referenced this pull request Sep 12, 2020
Follow #8699 (comment)
and #8606.

This PR uses `RESTRICT_ON_SEND` for `Lint/IdentityComparison`.
jmkoni pushed a commit to standardrb/standard that referenced this pull request Sep 16, 2020
…en fixed

* Update Rubocop from
  [0.90](https://github.com/rubocop-hq/rubocop/releases/tag/v0.90.0)
  to
  [0.91](https://github.com/rubocop-hq/rubocop/releases/tag/v0.91.0),
  enabling:
  * [`Lint/UselessTimes`](rubocop/rubocop#8702)
  * [`Layout/BeginEndAlignment`](rubocop/rubocop#8628)
  * [`Lint/ConstantDefinitionInBlock`](rubocop/rubocop#8707)
  * [`Lint/IdentityComparison`](rubocop/rubocop#8699)
  re-enabling after bug fixes:
  * [`Bundler/DuplicatedGem`](rubocop/rubocop#8666)
  * [`Naming/BinaryOperatorParameterName`](rubocop/rubocop#8664)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants