Skip to content

Commit

Permalink
[Fix rubocop#11792] Fix an error for Lint/DuplicateMatchPattern
Browse files Browse the repository at this point in the history
Fixes rubocop#11792.

This PR fixes an error for `Lint/DuplicateMatchPattern`
when using hash pattern with `if` guard.
  • Loading branch information
koic authored and bbatsov committed Apr 12, 2023
1 parent 5befa36 commit c59e349
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_duplicate_match_pattern.md
@@ -0,0 +1 @@
* [#11792](https://github.com/rubocop/rubocop/issues/11792): Fix an error for `Lint/DuplicateMatchPattern` when using hash pattern with `if` guard. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/duplicate_match_pattern.rb
Expand Up @@ -107,7 +107,7 @@ def on_case_match(case_node)

def pattern_identity(pattern)
pattern_source = if pattern.hash_pattern_type? || pattern.match_alt_type?
pattern.children.map(&:source).sort
pattern.children.map(&:source).sort.to_s
else
pattern.source
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/lint/duplicate_match_pattern_spec.rb
Expand Up @@ -214,4 +214,12 @@
end
RUBY
end

it 'does not crash when using hash pattern with `if` guard' do
expect_no_offenses(<<~RUBY)
case x
in { key: value } if condition
end
RUBY
end
end

0 comments on commit c59e349

Please sign in to comment.