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/EmptyInPattern cop #9825

Merged
merged 1 commit into from May 27, 2021

Conversation

koic
Copy link
Member

@koic koic commented May 26, 2021

This PR adds new Lint/EmptyInPattern cop for Ruby 2.7's pattern matching.
It checks for the presence of in branches without a body.

# bad
case condition
in [a]
  do_something
in [a, b]
end

# good
case condition
in [a]
  do_something
in [a, b]
  nil
end

# good - AllowComments: true (default)
case condition
in [a]
  do_something
in [a, b]
  # noop
end

# bad - AllowComments: false
case condition
in [a]
  do_something
in [a, b]
  # noop
end

This cop is similar to Lint/EmptyWhen, but with different supported syntax and Ruby version (requires 2.7 or higher).

And this PR use rubocop/rubocop-ast#183 feature, so it requires RuboCop AST 1.6.0 or higher.


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.


def on_case_match(node)
node.in_pattern_branches.each do |branch|
next if branch.node_parts[-1] || cop_config['AllowComments'] && comment_lines?(node)
Copy link
Member Author

Choose a reason for hiding this comment

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

When rubocop/rubocop-ast#183 can be used, branch.node_parts[-1] can be updated to branch.body.

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated it.

This PR adds new `Lint/EmptyInPattern` cop for Ruby 2.7's pattern matching.
It checks for the presence of `in` branches without a body.

```ruby
# bad
case condition
in [a]
  do_something
in [a, b]
end

# good
case condition
in [a]
  do_something
in [a, b]
  nil
end

# good - AllowComments: true (default)
case condition
in [a]
  do_something
in [a, b]
  # noop
end

# bad - AllowComments: false
case condition
in [a]
  do_something
in [a, b]
  # noop
end
```

This cop is similar to `Lint/EmptyWhen`, but with different supported
syntax and Ruby version (requires 2.7 or higher).

And this PR use rubocop/rubocop-ast#183 feature,
so it requires RuboCop AST 1.6.0 or higher.
@koic koic force-pushed the add_new_lint_empty_in_pattern_cop branch from f1cb713 to 0cc37f6 Compare May 26, 2021 06:29
@bbatsov bbatsov merged commit 2b27105 into rubocop:master May 27, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented May 27, 2021

Looks good!

@koic koic deleted the add_new_lint_empty_in_pattern_cop branch May 27, 2021 06:26
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