Sha256: 1352d41650ec56ff038bd4f88a876a060b7956d4cc805df080fbfd3ea39b40af

Contents?: true

Size: 1.42 KB

Versions: 175

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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)
      #
      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.'

        # @!method id_as_hash_key?(node)
        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

175 entries across 166 versions & 15 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/hash_compare_by_identity.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/hash_compare_by_identity.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/hash_compare_by_identity.rb