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

[Fix #10180] Fix an error for Style/SelectByRegexp #10181

Merged
merged 1 commit into from Oct 12, 2021
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
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