diff --git a/CHANGELOG.md b/CHANGELOG.md index eca06643662..75d6720fae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/lint/identity_comparison.rb b/lib/rubocop/cop/lint/identity_comparison.rb index c1b6d6d74ca..eed7956f75f 100644 --- a/lib/rubocop/cop/lint/identity_comparison.rb +++ b/lib/rubocop/cop/lint/identity_comparison.rb @@ -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 diff --git a/spec/rubocop/cop/lint/identity_comparison_spec.rb b/spec/rubocop/cop/lint/identity_comparison_spec.rb index 8f3e7b7056b..8699b3fbed8 100644 --- a/spec/rubocop/cop/lint/identity_comparison_spec.rb +++ b/spec/rubocop/cop/lint/identity_comparison_spec.rb @@ -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