Sha256: ecb84ce14fd9842b251050d8c1ec5cb4027c6100642f883bd321de7c197d8295
Contents?: true
Size: 997 Bytes
Versions: 21
Compression:
Stored size: 997 Bytes
Contents
# 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
Version data entries
21 entries across 21 versions & 1 rubygems