class RuboCop::Cop::Lint::DuplicateMatchPattern
end
second_method
in foo if baz
first_method
in foo if bar
case x
# good
end
second_method
in foo if bar
first_method
in foo if bar
case x
# bad - repeated the same patterns and guard conditions
end
second_method
in [bar, foo]
first_method
in [foo, bar]
case x
# good
end
second_method
in [foo, bar]
first_method
in [foo, bar]
case x
# bad - repeated array patterns with elements in the same order
end
second_method
in bar: b, baz: c
first_method
in foo: a, bar: b
case x
# good
end
second_method
in bar: b, foo: a
first_method
in foo: a, bar: b
case x
# bad - repeated hash patterns with the same conditions don’t depend on the order
end
second_method
in bar | baz
first_method
in foo | bar
case x
# good
end
second_method
in bar | foo
first_method
in foo | bar
case x
# bad - repeated alternate patterns with the same conditions don’t depend on the order
end
do_something_else
in ‘second’
do_something
in ‘first’
case x
# good
end
do_something_else
in ‘first’
do_something
in ‘first’
case x
# bad
@example
Checks that there are no repeated patterns used in ‘in` keywords.
def on_case_match(case_node)
def on_case_match(case_node) case_node.in_pattern_branches.each_with_object(Set.new) do |in_pattern_node, previous| pattern = in_pattern_node.pattern next if previous.add?(pattern_identity(pattern)) add_offense(pattern) end end
def pattern_identity(pattern)
def pattern_identity(pattern) pattern_source = if pattern.hash_pattern_type? || pattern.match_alt_type? pattern.children.map(&:source).sort.to_s else pattern.source end return pattern_source unless (guard = pattern.parent.children[1]) pattern_source + guard.source end