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

Support autocorrection on Performance/RedundantMatch when receiver is a Regexp literal #297

Merged
merged 1 commit into from Jul 27, 2022
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
@@ -0,0 +1 @@
* [#297](https://github.com/rubocop/rubocop-performance/pull/297): Support autocorrection on `Performance/RedundantMatch` when receiver is a Regexp literal. ([@r7kamura][])
12 changes: 7 additions & 5 deletions lib/rubocop/cop/performance/redundant_match.rb
Expand Up @@ -41,21 +41,23 @@ def on_send(node)
!(node.parent && node.parent.block_type?)

add_offense(node) do |corrector|
autocorrect(corrector, node)
autocorrect(corrector, node) if autocorrectable?(node)
end
end

private

def autocorrect(corrector, node)
# Regexp#match can take a second argument, but this cop doesn't
# register an offense in that case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave the comment to node.first_argument.regexp_type??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roger.
Actually, I don't fully understand why this comment is necessary here. Why does this comment mention the second argument? String#match may also take a second argument, so does it need a similar comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just keeps the status quo for future maintenance. No text updates would be needed.

return unless node.first_argument.regexp_type?

new_source = "#{node.receiver.source} =~ #{node.first_argument.source}"

corrector.replace(node.source_range, new_source)
end

def autocorrectable?(node)
# Regexp#match can take a second argument, but this cop doesn't
# register an offense in that case
node.receiver.regexp_type? || node.first_argument.regexp_type?
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/performance/redundant_match_spec.rb
Expand Up @@ -111,4 +111,15 @@ def method(str)
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY
end

it 'registers an offense and corrects when receiver is a Regexp literal' do
expect_offense(<<~RUBY)
something if /regex/.match(str)
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used.
RUBY

expect_correction(<<~RUBY)
something if /regex/ =~ str
RUBY
end
end