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

Conversation

@owst
Copy link
Contributor

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 owst:add_style_redundant_regexp_character_class_cop branch from 0963bfe to 3d44719 May 28, 2020
@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

marcandre commented May 30, 2020

I also wouldn't have regexp cops separated.

@@ -3,6 +3,7 @@
## master (unreleased)

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

This comment has been minimized.

Copy link
@bbatsov

bbatsov May 31, 2020

Collaborator

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

This comment has been minimized.

Copy link
@bbatsov

bbatsov May 31, 2020

Collaborator

true -> pending

include MatchRange

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

This comment has been minimized.

Copy link
@bbatsov

bbatsov May 31, 2020

Collaborator

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 owst:add_style_redundant_regexp_character_class_cop branch from 6001467 to 9cddd6b Jun 1, 2020
@owst
Copy link
Contributor Author

owst commented Jun 1, 2020

Done @bbatsov

@bbatsov bbatsov merged commit 2ccaf3e into rubocop-hq:master Jun 1, 2020
26 checks passed
26 checks passed
windows 2.4
Details
windows 2.5
Details
windows 2.6
Details
windows 2.7
Details
windows mingw
Details
ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
@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
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.