Skip to content

Commit

Permalink
[Fix rubocop#8720] Fix an error for Lint/IdentityComparison
Browse files Browse the repository at this point in the history
Fixes rubocop#8720.

This PR fixes an error for `Lint/IdentityComparison` when
calling `object_id` method without receiver in LHS or RHS.
  • Loading branch information
koic committed Sep 15, 2020
1 parent 03e83a2 commit a0edb50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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

0 comments on commit a0edb50

Please sign in to comment.