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 Style/RedundantRegexpCharacterClass cop #8055

Merged

Conversation

owst
Copy link
Contributor

@owst owst commented May 28, 2020

This PR adds a cop to check for unnecessary single-element Regexp character classes:

  # This cop checks for unnecessary single-element Regexp character classes.
  #
  # @example
  #
  #   # bad
  #   r = /[x]/
  #
  #   # good
  #   r = /x/
  #
  #   # bad
  #   r = /[\s]/
  #
  #   # good
  #   r = /\s/
  #
  #   # good
  #   r = /[ab]/

Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@owst owst force-pushed the add_style_redundant_regexp_character_class_cop branch from 0963bfe to 3d44719 Compare May 28, 2020 22:17
@koic
Copy link
Member

koic commented May 29, 2020

It sounds like a good idea, but maybe it's better to suggest it to Rubocop::RegularExpression.
https://github.com/pocke/rubocop-regular_expression

cc @pocke

@owst
Copy link
Contributor Author

owst commented May 30, 2020

Hmm - is rubocop-regular_expression released? It looks a little like a work in progress (but I like the cop it adds 👍 ).

I'm also proposing to add another related Regexp cop in #7908 and Rubocop already has

Lint/AmbiguousRegexpLiteral
Lint/RegexpAsCondition
Lint/UriRegexp
Style/RegexpLiteral

which are Regexp-related.

What do you think about adding this (and #7908) and maybe(?) later extracting them into rubocop-regular_expression if it is released?

@bbatsov
Copy link
Collaborator

bbatsov commented May 30, 2020

For the record - I'm fine with adding more regular expression cops to the main gem. Looking at #7749 I assume that @pocke is fine with this approach as well.

@marcandre
Copy link
Contributor

I also wouldn't have regexp cops separated.

CHANGELOG.md Outdated
@@ -3,6 +3,7 @@
## master (unreleased)

### New features
* [#8055](https://github.com/rubocop-hq/rubocop/pull/8055): Add new `Style/RedundantRegexpCharacterClass` cop. ([@owst][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a blank line after the heading and remove the blank line between this and the next changelog entry?

@@ -3607,6 +3607,11 @@ Style/RedundantPercentQ:
Enabled: true
VersionAdded: '0.76'

Style/RedundantRegexpCharacterClass:
Description: 'Checks for unnecessary single-element Regexp character classes.'
Enabled: true
Copy link
Collaborator

Choose a reason for hiding this comment

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

true -> pending

include MatchRange

MSG_REDUNDANT_CHARACTER_CLASS =
'Redundant single-element character class, `[` and `]` can be removed'
Copy link
Collaborator

Choose a reason for hiding this comment

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

You forgot the final .. I'd also make this more actionable and word it like Redundant ... [x] can be replaced with x. (or something along those lines).

@bbatsov
Copy link
Collaborator

bbatsov commented May 31, 2020

Great cop, btw! I love it!

@koic
Copy link
Member

koic commented May 31, 2020

Looking at #7749 I assume that @pocke is fine with this approach as well.

I forgot #7749. Certainly this cop could be included in the core. Thank you!

@bbatsov
Copy link
Collaborator

bbatsov commented May 31, 2020

@owst You also have to rebase on top of master.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 1, 2020

@owst The CI is failing now.

@owst
Copy link
Contributor Author

owst commented Jun 1, 2020

@bbatsov I needed a tweak to the cop added in #7908 - see #8073

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 1, 2020

@owst I guess you have to rebase and squash here and we're good to go.

```ruby
  # This cop checks for unnecessary single-element Regexp character classes.
  #
  # @example
  #
  #   # bad
  #   r = /[x]/
  #
  #   # good
  #   r = /x/
  #
  #   # bad
  #   r = /[\s]/
  #
  #   # good
  #   r = /\s/
  #
  #   # good
  #   r = /[ab]/
```
@owst owst force-pushed the add_style_redundant_regexp_character_class_cop branch from 6001467 to 9cddd6b Compare June 1, 2020 09:33
@owst
Copy link
Contributor Author

owst commented Jun 1, 2020

Done @bbatsov

@bbatsov bbatsov merged commit 2ccaf3e into rubocop:master Jun 1, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 1, 2020

Thanks!

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

4 participants