class RuboCop::Cop::Style::CaseEquality
self.class === something
# good
@example AllowOnSelfClass: true
self.class === something
# bad
@example AllowOnSelfClass: false (default)
Array === something
# good
@example AllowOnConstant: true
Array === something
# bad
@example AllowOnConstant: false (default)
/something/.match?(some_string)
(1..100).include?(7)
something.is_a?(Array)
# good
/something/ === some_string
(1..100) === 7
# bad
@example
the case equality operator is ‘self.class`. Note intermediate variables are not accepted.
If `AllowOnSelfClass` option is enabled, the cop will ignore violations when the receiver of
def begin_replacement(lhs, rhs)
def begin_replacement(lhs, rhs) return unless lhs.children.first&.range_type? "#{lhs.source}.include?(#{rhs.source})" end
def const_replacement(lhs, rhs)
def const_replacement(lhs, rhs) "#{rhs.source}.is_a?(#{lhs.source})" end
def offending_receiver?(node)
def offending_receiver?(node) return false if node&.const_type? && cop_config.fetch('AllowOnConstant', false) return false if self_class?(node) && cop_config.fetch('AllowOnSelfClass', false) true end
def on_send(node)
def on_send(node) case_equality?(node) do |lhs, rhs| return if lhs.const_type? && !lhs.module_name? add_offense(node.loc.selector) do |corrector| replacement = replacement(lhs, rhs) corrector.replace(node, replacement) if replacement end end end
def replacement(lhs, rhs)
def replacement(lhs, rhs) case lhs.type when :regexp # The automatic correction from `a === b` to `a.match?(b)` needs to # consider `Regexp.last_match?`, `$~`, `$1`, and etc. # This correction is expected to be supported by `Performance/Regexp` cop. # See: https://github.com/rubocop/rubocop-performance/issues/152 # # So here is noop. when :begin begin_replacement(lhs, rhs) when :const const_replacement(lhs, rhs) when :send send_replacement(lhs, rhs) end end
def send_replacement(lhs, rhs)
def send_replacement(lhs, rhs) return unless self_class?(lhs) "#{rhs.source}.is_a?(#{lhs.source})" end