class RuboCop::Cop::Style::MultipleComparison

Checks against comparing a variable with multiple items, where `Array#include?`, `Set#include?` or a `case` could be used instead to avoid code repetition. It accepts comparisons of multiple method calls to avoid unnecessary method calls by default. It can be configured by `AllowMethodComparison` option.

@example

# bad
a = 'a'
foo if a == 'a' || a == 'b' || a == 'c'

# good
a = 'a'
foo if ['a', 'b', 'c'].include?(a)

VALUES = Set['a', 'b', 'c'].freeze
# elsewhere...
foo if VALUES.include?(a)

case foo
when 'a', 'b', 'c' then foo
# ...
end

# accepted (but consider `case` as above)
foo if a == b.lightweight || a == b.heavyweight

@example AllowMethodComparison: true (default)

# good
foo if a == b.lightweight || a == b.heavyweight

@example AllowMethodComparison: false

# bad
foo if a == b.lightweight || a == b.heavyweight

# good
foo if [b.lightweight, b.heavyweight].include?(a)

Constants

MSG

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 49
def on_new_investigation
  @last_comparison = nil
end
on_or(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 53
def on_or(node)
  reset_comparison if switch_comparison?(node)

  root_of_or_node = root_of_or_node(node)

  return unless node == root_of_or_node
  return unless nested_variable_comparison?(root_of_or_node)
  return if @allowed_method_comparison

  add_offense(node) do |corrector|
    elements = @compared_elements.join(', ')
    prefer_method = "[#{elements}].include?(#{variables_in_node(node).first})"

    corrector.replace(node, prefer_method)
  end

  @last_comparison = node
end

Private Instance Methods

allow_method_comparison?() click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 151
def allow_method_comparison?
  cop_config.fetch('AllowMethodComparison', true)
end
comparison?(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 126
def comparison?(node)
  simple_comparison_lhs?(node) || simple_comparison_rhs?(node) || nested_comparison?(node)
end
nested_comparison?(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 118
def nested_comparison?(node)
  if node.or_type?
    node.node_parts.all? { |node_part| comparison? node_part }
  else
    false
  end
end
nested_variable_comparison?(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 87
def nested_variable_comparison?(node)
  return false unless nested_comparison?(node)

  variables_in_node(node).count == 1
end
reset_comparison() click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 146
def reset_comparison
  @compared_elements = []
  @allowed_method_comparison = false
end
root_of_or_node(or_node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 130
def root_of_or_node(or_node)
  return or_node unless or_node.parent

  if or_node.parent.or_type?
    root_of_or_node(or_node.parent)
  else
    or_node
  end
end
switch_comparison?(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 140
def switch_comparison?(node)
  return true if @last_comparison.nil?

  @last_comparison.descendants.none?(node)
end
variable_name(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 114
def variable_name(node)
  node.children[0]
end
variables_in_node(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 93
def variables_in_node(node)
  if node.or_type?
    node.node_parts.flat_map { |node_part| variables_in_node(node_part) }.uniq
  else
    variables_in_simple_node(node)
  end
end
variables_in_simple_node(node) click to toggle source
# File lib/rubocop/cop/style/multiple_comparison.rb, line 101
def variables_in_simple_node(node)
  simple_double_comparison?(node) do |var1, var2|
    return [variable_name(var1), variable_name(var2)]
  end
  if (var, obj = simple_comparison_lhs?(node)) || (obj, var = simple_comparison_rhs?(node))
    @allowed_method_comparison = true if allow_method_comparison? && obj.send_type?
    @compared_elements << obj.source
    return [variable_name(var)]
  end

  []
end