Sha256: 6eb90877ac67c13b76bdd3aee4224dd309ab84d2e54d488c7d067fed3ab45b35

Contents?: true

Size: 1.35 KB

Versions: 117

Compression:

Stored size: 1.35 KB

Contents

# 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`.'
        RESTRICT_ON_SEND = %i[==].freeze

        def on_send(node)
          return unless compare_between_object_id_by_double_equal?(node)

          add_offense(node) do |corrector|
            receiver = node.receiver.receiver
            argument = node.first_argument.receiver
            return unless receiver && argument

            replacement = "#{receiver.source}.equal?(#{argument.source})"

            corrector.replace(node, replacement)
          end
        end

        private

        def compare_between_object_id_by_double_equal?(node)
          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

Version data entries

117 entries across 111 versions & 11 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/identity_comparison.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/identity_comparison.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/identity_comparison.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.52.0 lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.51.0 lib/rubocop/cop/lint/identity_comparison.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.50.2 lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.50.1 lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.50.0 lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.49.0 lib/rubocop/cop/lint/identity_comparison.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/rubocop-1.48.1/lib/rubocop/cop/lint/identity_comparison.rb
rubocop-1.48.1 lib/rubocop/cop/lint/identity_comparison.rb