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 picks wrong regex #8708

Closed
ysbaddaden opened this issue Sep 13, 2020 · 1 comment · Fixed by #8844
Closed

Lint/OutOfRangeRegexpRef picks wrong regex #8708

ysbaddaden opened this issue Sep 13, 2020 · 1 comment · Fixed by #8844

Comments

@ysbaddaden
Copy link

Actual behavior

Lint/OutOfRangeRegexpRef seems confused when multiple regular expressions are present on a single line, and picks the wrong Regex, leading to false positives and true negatives.

Steps to reproduce the problem

In the following example Lint/OutOfRangeRegexpRef seems to pick the first Regex, in the gsub call (/\s+/), instead of the final Regex that is populating $1 (/foo(bar)/):

if "foo bar".gsub(/\s+/, "") =~ /foo(bar)/
  p $1
end
x.rb:2:5: W: Lint/OutOfRangeRegexpRef: Do not use out of range reference for the Regexp.
  p $1
    ^^

Rubocop won't complain anymore when the Regex in the gsub call is modified to capture, but still won't complain when changing the second regex to not capture:

if "foo bar".gsub(/(\s+)/, "") =~ /foobar/
  p $1 # always nil
end

RuboCop version

0.90.0 (using Parser 2.7.1.4, rubocop-ast 0.4.0, running on ruby 2.6.6 x86_64-linux-musl)
@ysbaddaden
Copy link
Author

Here is another weird example. This time Lint/OutOfRangeRegexpRef will pick the following /\s{2}/ Regex, despite being a match call on $1 itself:

if "some : line " =~ / : (.+)/
  $1.gsub(/\s{2}/, " ")
end
x.rb:2:3: W: Lint/OutOfRangeRegexpRef: Do not use out of range reference for the Regexp.
  $1.gsub(/\s{2}/, " ")
  ^^

@ysbaddaden ysbaddaden changed the title Lint/OutOfRangeRegexpRef picks wrong regex in chained expressions Lint/OutOfRangeRegexpRef picks wrong regex Sep 13, 2020
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Oct 26, 2020
marcandre pushed a commit that referenced this issue Oct 26, 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
1 participant