Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Lint/DuplicateMatchPattern cop #11749

Merged
merged 1 commit into from
Apr 3, 2023

Conversation

koic
Copy link
Member

@koic koic commented Apr 1, 2023

This PR adds new Lint/DuplicateMatchPattern cop. It checks that there are no repeated patterns used in in keywords.

# bad
case x
in 'first'
  do_something
in 'first'
  do_something_else
end

# good
case x
in 'first'
  do_something
in 'second'
  do_something_else
end

I was wondering whether to extend existing Lint/DuplicateCaseCondition, but it is a different inspection target from the concept named "case condition". So, I implemented it as a new Lint/DuplicateMatchPattern for "match pattern".


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

module Lint
# Checks that there are no repeated patterns used in `in` keywords.
#
# @example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to show examples with more complex patterns, so it's clear that this cop is pretty smart about them.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 1, 2023

I wonder if DuplicateMatchPattern might not be a better name for the cop (or at least a more descriptive name).

This PR adds new `Lint/DuplicateMatchPattern` cop.
It checks that there are no repeated patterns used in `in` keywords.

```ruby
# bad
case x
in 'first'
  do_something
in 'first'
  do_something_else
end

# good
case x
in 'first'
  do_something
in 'second'
  do_something_else
end
```

I was wondering whether to extend existing `Lint/DuplicateCaseCondition`,
but it is a different inspection target from the concept named "`case` condition".
So, I implemented it as a new `Lint/DuplicateMatchPattern` for "match pattern".
@koic koic changed the title Add new Lint/DuplicateInPattern cop Add new Lint/DuplicateMatchPattern cop Apr 3, 2023
@koic koic force-pushed the add_new_lint_duplicate_in_pattern_cop branch from a3bb23d to a910f15 Compare April 3, 2023 08:33
@koic
Copy link
Member Author

koic commented Apr 3, 2023

Thank you for the suggestions. DuplicateMatchPattern looks like a better name and I've also updated the examples.

@bbatsov bbatsov merged commit f8417db into rubocop:master Apr 3, 2023
12 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 3, 2023

Looks great. Thanks!

@koic koic deleted the add_new_lint_duplicate_in_pattern_cop branch April 3, 2023 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants