Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/HashCompareByIdentity
cop
- Loading branch information
Showing
10 changed files
with
127 additions
and
8 deletions.
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
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# Prefer using `Hash#compare_by_identity` than using `object_id` for hash keys. | ||
# | ||
# This cop is marked as unsafe as a hash possibly can contain other keys | ||
# besides `object_id`s. | ||
# | ||
# @example | ||
# # bad | ||
# hash = {} | ||
# hash[foo.object_id] = :bar | ||
# hash.key?(baz.object_id) | ||
# | ||
# # good | ||
# hash = {}.compare_by_identity | ||
# hash[foo] = :bar | ||
# hash.key?(baz) | ||
# | ||
class HashCompareByIdentity < Base | ||
RESTRICT_ON_SEND = %i[key? has_key? fetch [] []=].freeze | ||
|
||
MSG = 'Use `Hash#compare_by_identity` instead of using `object_id` for keys.' | ||
|
||
def_node_matcher :id_as_hash_key?, <<~PATTERN | ||
(send _ {:key? :has_key? :fetch :[] :[]=} (send _ :object_id) ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
add_offense(node) if id_as_hash_key?(node) | ||
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::HashCompareByIdentity do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when using hash methods with `object_id` on receiver as a key' do | ||
expect_offense(<<~RUBY) | ||
hash.key?(foo.object_id) | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
hash.has_key?(foo.object_id) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
hash[foo.object_id] = bar | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
hash[foo.object_id] | ||
^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
hash.fetch(foo.object_id, 42) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using hash method with `object_id` as a key' do | ||
expect_offense(<<~RUBY) | ||
hash.key?(object_id) | ||
^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for hash methods without `object_id` as key' do | ||
expect_no_offenses(<<~RUBY) | ||
hash.key?(foo) | ||
RUBY | ||
end | ||
end |