class RuboCop::Cop::Style::MultipleComparison
foo if [b.lightweight, b.heavyweight].include?(a)
# good
foo if a == b.lightweight || a == b.heavyweight
# bad
@example AllowMethodComparison: false
foo if a == b.lightweight || a == b.heavyweight
# good
@example AllowMethodComparison: true (default)
foo if a == b.lightweight || a == b.heavyweight
# accepted (but consider ‘case` as above)
end
# …
when ’a’, ‘b’, ‘c’ then foo
case foo
foo if VALUES.include?(a)
# elsewhere…
VALUES = Set[‘a’, ‘b’, ‘c’].freeze
foo if [‘a’, ‘b’, ‘c’].include?(a)
a = ‘a’
# good
foo if a == ‘a’ || a == ‘b’ || a == ‘c’
a = ‘a’
# bad
@example
by default. It can be configured by ‘AllowMethodComparison` option.
It accepts comparisons of multiple method calls to avoid unnecessary method calls
to avoid code repetition.
`Array#include?`, `Set#include?` or a `case` could be used instead
Checks against comparing a variable with multiple items, where
def allow_method_comparison?
def allow_method_comparison? cop_config.fetch('AllowMethodComparison', true) end
def comparison?(node)
def comparison?(node) simple_comparison_lhs?(node) || simple_comparison_rhs?(node) || nested_comparison?(node) end
def nested_comparison?(node)
def nested_comparison?(node) if node.or_type? node.node_parts.all? { |node_part| comparison? node_part } else false end end
def nested_variable_comparison?(node)
def nested_variable_comparison?(node) return false unless nested_comparison?(node) variables_in_node(node).count == 1 end
def on_new_investigation
def on_new_investigation @last_comparison = nil end
def on_or(node)
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
def reset_comparison
def reset_comparison @compared_elements = [] @allowed_method_comparison = false end
def root_of_or_node(or_node)
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
def switch_comparison?(node)
def switch_comparison?(node) return true if @last_comparison.nil? @last_comparison.descendants.none?(node) end
def variable_name(node)
def variable_name(node) node.children[0] end
def variables_in_node(node)
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
def variables_in_simple_node(node)
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