Skip to content

Commit

Permalink
Merge pull request #12781 from Earlopain/fix-error-for-style-exact-re…
Browse files Browse the repository at this point in the history
…gexp-match

Fix an error for `Style/ExactRegexpMatch` when calling `match` without a receiver
  • Loading branch information
koic committed Mar 11, 2024
2 parents d2eb99c + 9585ace commit e93a1f0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_exact_regexp_match.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12781](https://github.com/rubocop/rubocop/pull/12781): Fix an error for `Style/ExactRegexpMatch` when calling `match` without a receiver. ([@earlopain][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/exact_regexp_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ class ExactRegexpMatch < Base
PATTERN

def on_send(node)
return unless (receiver = node.receiver)
return unless (regexp = exact_regexp_match(node))

parsed_regexp = Regexp::Parser.parse(regexp)
return unless exact_match_pattern?(parsed_regexp)

prefer = "#{node.receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
prefer = "#{receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"

add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
corrector.replace(node, prefer)
Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/cop/style/exact_regexp_match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
RUBY
end

it 'does not register an offense when using match without receiver' do
expect_no_offenses('match(/\\Astring\\z/)')
end

it 'registers an offense when using `string.match?(/\Astring\z/)`' do
expect_offense(<<~'RUBY')
string.match?(/\Astring\z/)
Expand Down

0 comments on commit e93a1f0

Please sign in to comment.