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

Style/RedundantRegexpCharacterClass wrong suggestion on v0.93.0+ #8915

Closed
cetinajero opened this issue Oct 21, 2020 · 4 comments
Closed

Style/RedundantRegexpCharacterClass wrong suggestion on v0.93.0+ #8915

cetinajero opened this issue Oct 21, 2020 · 4 comments

Comments

@cetinajero
Copy link
Contributor

cetinajero commented Oct 21, 2020

There was introduced a little bug on v0.93.0 that it is suggesting wrong auto-corrections on certain regex scenarios even when using rubocop -a (safe auto-correct).

[Corrected] Style/RedundantRegexpCharacterClass: Redundant single-element character class, [\-]{1} can be replaced with \-]{1.
            format: { with: /\A[a-z0-9]+([\-]{1}[a-z0-9]+)*\z/, message: I18n.t('alias.is_invalid') }
                                         ^^^^^^^

Steps to reproduce the problem

Environment

$ ruby -v
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]
$ ls
$ echo 'regex = /\A[a-z0-9]+([\-]{1}[a-z0-9]+)*\z/' > test.rb
$ ls
test.rb

v0.92.0 (working fine)

$ ls
test.rb
$ gem install rubocop -v 0.92.0
Fetching rubocop-0.92.0.gem
Successfully installed rubocop-0.92.0
Parsing documentation for rubocop-0.92.0
Installing ri documentation for rubocop-0.92.0
Done installing documentation for rubocop after 9 seconds
1 gem installed

$ rubocop --only Style/RedundantRegexpCharacterClass
Inspecting 1 file
C

Offenses:

test.rb:1:22: C: Style/RedundantRegexpCharacterClass: Redundant single-element character class, [\-] can be replaced with \-.
regex = /\A[a-z0-9]+([\-]{1}[a-z0-9]+)*\z/
                     ^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

v0.93.0 (bug)

$ ls
test.rb
$ gem install rubocop -v 0.93.0
Fetching rubocop-0.93.0.gem
Successfully installed rubocop-0.93.0
Parsing documentation for rubocop-0.93.0
Installing ri documentation for rubocop-0.93.0
Done installing documentation for rubocop after 9 seconds
1 gem installed

$ rubocop --only Style/RedundantRegexpCharacterClass
Inspecting 1 file
C

Offenses:

test.rb:1:22: C: Style/RedundantRegexpCharacterClass: Redundant single-element character class, [\-]{1} can be replaced with \-]{1.
regex = /\A[a-z0-9]+([\-]{1}[a-z0-9]+)*\z/
                     ^^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

RuboCop version

$ rubocop -V
0.93.0 (using Parser 2.7.2.0, rubocop-ast 1.0.0, running on ruby 2.6.6 x86_64-darwin19)
@cetinajero
Copy link
Contributor Author

The bug seems to be introduced when the cop was updated to use regexp_parser (PR #8625).

@owst
Copy link
Contributor

owst commented Oct 21, 2020

Hi @cetinajero - thanks for the report, and apologies for the bug. I think this will be fixed by #8913

@cetinajero
Copy link
Contributor Author

Great to know there is already an in-progress PR @owst!

@koic
Copy link
Member

koic commented Oct 30, 2020

#8913 has been merged and released with the latest RuboCop. Thank you.

@koic koic closed this as completed Oct 30, 2020
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

No branches or pull requests

3 participants