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

[Fix #8720] Fix an error for Lint/IdentityComparison #8722

Merged
merged 1 commit into from Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#8720](https://github.com/rubocop-hq/rubocop/issues/8720): Fix an error for `Lint/IdentityComparison` when calling `object_id` method without receiver in LHS or RHS. ([@koic][])

## 0.91.0 (2020-09-15)

### New features
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/lint/identity_comparison.rb
Expand Up @@ -26,9 +26,11 @@ def on_send(node)
return unless compare_between_object_id_by_double_equal?(node)

add_offense(node) do |corrector|
receiver = node.receiver.receiver.source
argument = node.first_argument.receiver.source
replacement = "#{receiver}.equal?(#{argument})"
receiver = node.receiver.receiver
argument = node.first_argument.receiver
return unless receiver && argument

replacement = "#{receiver.source}.equal?(#{argument.source})"

corrector.replace(node, replacement)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/identity_comparison_spec.rb
Expand Up @@ -31,4 +31,16 @@
foo.equal(bar)
RUBY
end

it 'does not register an offense when lhs is `object_id` without receiver' do
expect_no_offenses(<<~RUBY)
object_id == bar.object_id
RUBY
end

it 'does not register an offense when rhs is `object_id` without receiver' do
expect_no_offenses(<<~RUBY)
foo.object_id == object_id
RUBY
end
end