forked from rubocop/rubocop
/
identity_comparison.rb
51 lines (42 loc) · 1.35 KB
/
identity_comparison.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
#
# Prefer `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.
#
# @example
# # bad
# foo.object_id == bar.object_id
#
# # good
# foo.equal?(bar)
#
class IdentityComparison < Base
extend AutoCorrector
MSG = 'Use `equal?` instead `==` when comparing `object_id`.'
RESTRICT_ON_SEND = %i[==].freeze
def on_send(node)
return unless compare_between_object_id_by_double_equal?(node)
add_offense(node) do |corrector|
receiver = node.receiver.receiver
argument = node.first_argument.receiver
return unless receiver && argument
replacement = "#{receiver.source}.equal?(#{argument.source})"
corrector.replace(node, replacement)
end
end
private
def compare_between_object_id_by_double_equal?(node)
object_id_method?(node.receiver) && object_id_method?(node.first_argument)
end
def object_id_method?(node)
node.send_type? && node.method?(:object_id)
end
end
end
end
end