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

Lint/OutOfRangeRegexpRef does not detect captures from pattern matching syntax. #10976

Closed
jasonkarns opened this issue Aug 29, 2022 · 1 comment · Fixed by #11337
Closed

Lint/OutOfRangeRegexpRef does not detect captures from pattern matching syntax. #10976

jasonkarns opened this issue Aug 29, 2022 · 1 comment · Fixed by #11337

Comments

@jasonkarns
Copy link

The Lint/OutOfRangeRegexpRef does not seem to recognize regular expressions (which have capture groups) that are used in Ruby's new pattern matching syntax. The cop fails to recognize captures when the regex is used in a case-in expression as well as the => array syntax.

Both forms invoke Regexp#=== under the hood, which still trigger's ruby's Regexp.last_match capabilities (as well as setting the 'last-match' special global variables like $1). Since the variables are set from the captured groups, I would expect rubocop's linter to recognize them as valid capture references.


Expected behavior

Ruby's new pattern matching syntax accepts regular expressions and captures groups. It still stores captures in the special globals as documented here: https://ruby-doc.org/core-3.1.2/Regexp.html#class-Regexp-label-Special+global+variables

case "rubocop"
in /rubo(.*)/ then p $1 # prints "cop"
end
"rubocop" => /rubo(.*)/
p $1 # prints "cop"

Actual behavior

Rubocop doesn't recognize the regex pattern in the "case-in", nor "arrow" pattern matching syntax.

in both cases, it prints:

  app/lib/vtiger/webservice/id.rb:6:39: Lint/OutOfRangeRegexpRef: $1 is out of range (no regexp capture groups detected).

Steps to reproduce the problem

The two ruby snippets above will reproduce the cop error.

RuboCop version

    rubocop (1.35.0)
    rubocop-ast (1.21.0)
    rubocop-performance (1.14.3)
    standard (1.16.0)
@jasonkarns
Copy link
Author

I presume this is just an issue with rubocop-ast, but assume the issue was better reported here?

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 a pull request may close this issue.

1 participant