Skip to content

Commit

Permalink
[Fix rubocop#10180] Fix an error for Style/SelectByRegexp
Browse files Browse the repository at this point in the history
Fixes rubocop#10180.

This PR fixes an error for `Style/SelectByRegexp` when using `match?` without a receiver.
  • Loading branch information
koic committed Oct 11, 2021
1 parent 512b771 commit abcf9d3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_select_by_regexp.md
@@ -0,0 +1 @@
* [#10180](https://github.com/rubocop/rubocop/issues/10180): Fix an error for `Style/SelectByRegexp` when using `match?` without a receiver. ([@koic][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/select_by_regexp.rb
Expand Up @@ -83,6 +83,7 @@ def on_send(node)
return if block_node.body.begin_type?
return if receiver_allowed?(block_node.receiver)
return unless (regexp_method_send_node = extract_send_node(block_node))
return if match_predicate_without_receiver?(regexp_method_send_node)

regexp = find_regexp(regexp_method_send_node)
register_offense(node, block_node, regexp)
Expand Down Expand Up @@ -127,6 +128,10 @@ def find_regexp(node)
node.receiver
end
end

def match_predicate_without_receiver?(node)
node.send_type? && node.method?(:match?) && node.receiver.nil?
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/select_by_regexp_spec.rb
Expand Up @@ -279,6 +279,12 @@
array.#{method} { /regexp/.match?(foo(_1)) }
RUBY
end

it 'does not register an offense when using `match?` without a receiver' do
expect_no_offenses(<<~RUBY)
array.#{method} { |item| match?(item) }
RUBY
end
end
end
end
Expand Down

0 comments on commit abcf9d3

Please sign in to comment.