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 flags single characters in interpolations #8098

Closed
beechtom opened this issue Jun 4, 2020 · 0 comments
Closed
Labels

Comments

@beechtom
Copy link

beechtom commented Jun 4, 2020

Expected behavior

The Style/RedundantRegexpCharacterClass should not flag or fix single characters in brackets in interpolations. Interpolations with bracket notation are typically used to access objects and should not be treated as single-element classes.

Actual behavior

The Style/RedundantRegexpCharacterClass flags and fixes single characters in brackets in interpolations. Interpolations with bracket notation is viewed as a single-element class and has the brackets removed. This results in the Lint/Syntax cop flagging the change due to unrecognized characters in the interpolation. See below.

Steps to reproduce the problem

Take this trivial script:

string = "one.two.three"
regexp = /#{string.split('.')[0]}/

puts regexp.match('one')

The expected behavior is that the regexp should resolve to /one/. However, running the Style/RedundantRegexpCharacterClass will flag [0] as a single-element class even though it is valid notation for accessing the first element of an array. Attempting to auto-correct with this cop results in the following change to the file:

string = "one.two.three"
regexp = /#{string.split('.')0}/

puts regexp.match('one')

The interpolation string.split('.')0 has invalid syntax and results in the Lint/Syntax cop flagging the change due to an unexpected integer:

E: Lint/Syntax: unexpected token tINTEGER
(Using Ruby 2.5 parser; configure using TargetRubyVersion parameter, under AllCops)
      regex = /#{string.split('.')0}/

RuboCop version

$ rubocop -V
0.85.0 (using Parser 2.7.1.3, rubocop-ast 0.0.3, running on ruby 2.6.3 x86_64-darwin19)
ivgiuliani added a commit to gocardless/gc_ruboconfig that referenced this issue Jun 10, 2020
Namely:

- `Lint/MixedRegexpCaptureTypes`
- `Style/RedundantRegexpCharacterClass` (disabled by default due to
  rubocop/rubocop#8098)
- `Style/RedundantRegexpEscape`
owst added a commit to owst/rubocop that referenced this issue Jun 10, 2020
…terClass

Also correct the handling of interpolations and comments - we need to
preserve their width in the checked pattern source without their
contents triggering the cop in question, so that we can correctly
highlight following offenses based on the offset in the pattern_source.
@koic koic added the bug label Jun 11, 2020
ivgiuliani added a commit to gocardless/gc_ruboconfig that referenced this issue Jun 11, 2020
Namely:

- `Lint/MixedRegexpCaptureTypes`
- `Style/RedundantRegexpCharacterClass` (disabled by default due to
  rubocop/rubocop#8098)
- `Style/RedundantRegexpEscape`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants