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

Set valid ref range when regexp is matched in Lint/OutOfRangeRegexpRef #8463

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#8463](https://github.com/rubocop-hq/rubocop/pull/8463): Fix false positives for `Lint/OutOfRangeRegexpRef` when a regexp is defined and matched in separate steps. ([@eugeneius][])

## 0.89.0 (2020-08-05)

### New features
Expand Down
25 changes: 21 additions & 4 deletions lib/rubocop/cop/lint/out_of_range_regexp_ref.rb
Expand Up @@ -21,16 +21,26 @@ module Lint
class OutOfRangeRegexpRef < Base
MSG = 'Do not use out of range reference for the Regexp.'

REGEXP_CAPTURE_METHODS = %i[=~ === match].to_set.freeze

def on_new_investigation
@valid_ref = 0
end

def on_regexp(node)
def on_match_with_lvasgn(node)
check_regexp(node.children.first)
end

def on_send(node)
return unless REGEXP_CAPTURE_METHODS.include?(node.method_name)

@valid_ref = nil
return if contain_non_literal?(node)

tree = Regexp::Parser.parse(node.content)
@valid_ref = regexp_captures(tree)
if node.receiver&.regexp_type?
check_regexp(node.receiver)
elsif node.first_argument&.regexp_type? && node.method?(:=~)
check_regexp(node.first_argument)
end
end

def on_nth_ref(node)
Expand All @@ -42,6 +52,13 @@ def on_nth_ref(node)

private

def check_regexp(regexp)
return if contain_non_literal?(regexp)

tree = Regexp::Parser.parse(regexp.content)
@valid_ref = regexp_captures(tree)
end

def contain_non_literal?(node)
node.children.size != 2 || !node.children.first.str_type?
end
Expand Down
55 changes: 55 additions & 0 deletions spec/rubocop/cop/lint/out_of_range_regexp_ref_spec.rb
Expand Up @@ -78,4 +78,59 @@
puts $2
RUBY
end

it 'registers an offense when the regexp comes after `=~`' do
expect_offense(<<~RUBY)
"foobar" =~ /(foo)(bar)/
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end

it 'registers an offense when the regexp is matched with `===`' do
expect_offense(<<~RUBY)
/(foo)(bar)/ === "foobar"
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end

it 'registers an offense when the regexp is matched with `match`' do
expect_offense(<<~RUBY)
/(foo)(bar)/.match("foobar")
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end

it 'ignores calls to `match?`' do
expect_offense(<<~RUBY)
/(foo)(bar)/.match("foobar")
/(foo)(bar)(baz)/.match?("foobarbaz")
puts $3
^^ Do not use out of range reference for the Regexp.
RUBY
end

it 'handles `match` with no arguments' do
expect_no_offenses(<<~RUBY)
foo.match
RUBY
end

it 'handles `match` with no receiver' do
expect_no_offenses(<<~RUBY)
match(bar)
RUBY
end

it 'only registers an offense when the regexp is matched as a literal' do
expect_no_offenses(<<~RUBY)
foo_bar_regexp = /(foo)(bar)/
foo_regexp = /(foo)/

foo_bar_regexp =~ 'foobar'
puts $2
RUBY
end
end