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

False positive in Style/RedundantRegexpEscape #11180

Closed
jfeltesse-mdsol opened this issue Nov 15, 2022 · 1 comment
Closed

False positive in Style/RedundantRegexpEscape #11180

jfeltesse-mdsol opened this issue Nov 15, 2022 · 1 comment
Labels

Comments

@jfeltesse-mdsol
Copy link

I believe #11152 introduced a false positive.


Expected behavior

Given the regex

%r{  remote: (?:git@|git://|https://)(?<domain>[^/]+)[/:](?<slug>[\w\-._]+/[\w\-._]+)}

rubocop shouldn't complain about the redundancy of slashes in the <slug> capture, as removing them leads to a syntax error:

unmatched range specifier in char-class: /  remote: (?:git@|git:\/\/|https:\/\/)(?<domain>[^\/]+)[\/:](?<slug>[\w-._]+\/[\w-._]+)/

Actual behavior

C: [Correctable] Style/RedundantRegexpEscape: Redundant escape inside regexp literal
      GIT_URI_REGEX = %r{  remote: (?:git@|git://|https://)(?<domain>[^/]+)[/:](?<slug>[\w\-._]+/[\w\-._]+)}
                                                                                          ^^

C: [Correctable] Style/RedundantRegexpEscape: Redundant escape inside regexp literal
      GIT_URI_REGEX = %r{  remote: (?:git@|git://|https://)(?<domain>[^/]+)[/:](?<slug>[\w\-._]+/[\w\-._]+)}
                                                                                                    ^^

Steps to reproduce the problem

  1. use rubocop 1.39.0 with above regex
  2. above offenses are reported
  3. use rubocop 1.38.0
  4. no offenses

RuboCop version

rubocop -V
1.39.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [arm64-darwin21]
  - rubocop-performance 1.15.0
  - rubocop-rspec 2.15.0
@koic koic added the bug label Nov 15, 2022
@olliebennett
Copy link
Contributor

olliebennett commented Nov 30, 2022

I boiled the provided REGEX down to some simpler examples;

  • %r{[\w\-x]} becomes %r{[\w-x]} which causes "unmatched range specifier in char-class" SyntaxError
  • %r{[x\-\w]} becomes %r{[x-\w]} which causes "char-class value at end of range" SyntaxError

The same applies when replacing \w with any of the character classes;

  • \s | Any whitespace character
  • \S | Any non-whitespace character
  • \d | Any digit
  • \D | Any non-digit
  • \w | Any word character (letter, number, underscore)
  • \W | Any non-word character
  • ... maybe other niche ones I'm not aware of?

Hope that helps when someone is able to write a spec and fix it 🤞 🙏

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

3 participants