Class: RuboCop::Cop::Style::ClassEqualityComparison

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
AllowedMethods, AllowedPattern, RangeHelp
Defined in:
lib/rubocop/cop/style/class_equality_comparison.rb

Overview

Enforces the use of ‘Object#instance_of?` instead of class comparison for equality. `==`, `equal?`, and `eql?` custom method definitions are allowed by default. These are customizable with `AllowedMethods` option.

Examples:

# bad
var.class == Date
var.class.equal?(Date)
var.class.eql?(Date)
var.class.name == 'Date'

# good
var.instance_of?(Date)

AllowedMethods: [‘==’, ‘equal?’, ‘eql?’] (default)

# good
def ==(other)
  self.class == other.class && name == other.name
end

def equal?(other)
  self.class.equal?(other.class) && name.equal?(other.name)
end

def eql?(other)
  self.class.eql?(other.class) && name.eql?(other.name)
end

AllowedPatterns: [] (default)

# bad
def eq(other)
  self.class.eq(other.class) && name.eq(other.name)
end

AllowedPatterns: [‘eq’]

# good
def eq(other)
  self.class.eq(other.class) && name.eq(other.name)
end

Constant Summary collapse

MSG =
'Use `instance_of?%<class_argument>s` instead of comparing classes.'
RESTRICT_ON_SEND =
%i[== equal? eql?].freeze
CLASS_NAME_METHODS =
%i[name to_s inspect].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#class_comparison_candidate?(node) ⇒ Object



64
65
66
67
68
# File 'lib/rubocop/cop/style/class_equality_comparison.rb', line 64

def_node_matcher :class_comparison_candidate?, <<~PATTERN
  (send
    {$(send _ :class) (send $(send _ :class) #class_name_method?)}
    {:== :equal? :eql?} $_)
PATTERN

#on_send(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rubocop/cop/style/class_equality_comparison.rb', line 70

def on_send(node)
  def_node = node.each_ancestor(:def, :defs).first
  return if def_node &&
            (allowed_method?(def_node.method_name) ||
            matches_allowed_pattern?(def_node.method_name))

  class_comparison_candidate?(node) do |receiver_node, class_node|
    return if class_node.dstr_type?

    range = offense_range(receiver_node, node)
    class_argument = (class_name = class_name(class_node, node)) ? "(#{class_name})" : ''

    add_offense(range, message: format(MSG, class_argument: class_argument)) do |corrector|
      next unless class_name

      corrector.replace(range, "instance_of?#{class_argument}")
    end
  end
end