class RuboCop::Cop::Lint::HashCompareByIdentity
Prefer using ‘Hash#compare_by_identity` rather than using `object_id` for hash keys.
This cop looks for hashes being keyed by objects’ ‘object_id`, using one of these methods: `key?`, `has_key?`, `fetch`, `[]` and `[]=`.
@safety
This cop is unsafe. Although unlikely, the hash could store both object ids and other values that need be compared by value, and thus could be a false positive. Furthermore, this cop cannot guarantee that the receiver of one of the methods (`key?`, etc.) is actually a hash.
@example
# bad hash = {} hash[foo.object_id] = :bar hash.key?(baz.object_id) # good hash = {}.compare_by_identity hash[foo] = :bar hash.key?(baz)
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/lint/hash_compare_by_identity.rb, line 41 def on_send(node) add_offense(node) if id_as_hash_key?(node) end
Also aliased as: on_csend