forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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`.' | ||
|
||
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})" | ||
|
||
corrector.replace(node, replacement) | ||
end | ||
end | ||
|
||
private | ||
|
||
def compare_between_object_id_by_double_equal?(node) | ||
return false unless node.method?(:==) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::IdentityComparison, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense and corrects when using `==` for comparison between `object_id`s' do | ||
expect_offense(<<~RUBY) | ||
foo.object_id == bar.object_id | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `equal?` instead `==` when comparing `object_id`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo.equal?(bar) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `==` for comparison between `object_id` and other' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.object_id == bar.do_something | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when a receiver that is not `object_id` uses `==`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.do_something == bar.object_id | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `==`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.equal(bar) | ||
RUBY | ||
end | ||
end |