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 a false negative for Performance/RegexpMatch #61

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 147
Max: 149

# Offense count: 14
# Configuration parameters: CountComments, ExcludedMethods.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#54](https://github.com/rubocop-hq/rubocop-performance/issues/54): Fix `Performance/FixedSize` to accept const assign with some operation. ([@tejasbubane][])
* [#61](https://github.com/rubocop-hq/rubocop-performance/pull/61): Fix a false negative for `Performance/RegexpMatch` when using RuboCop 0.71 or higher. ([@koic][])

## 1.3.0 (2019-05-13)

Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/performance/regexp_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ class RegexpMatch < Cop

def_node_matcher :match_method?, <<-PATTERN
{
(send _recv :match _ <int ...>)
(send _recv :match {regexp str sym})
(send {regexp str sym} :match _)
}
PATTERN

def_node_matcher :match_with_int_arg_method?, <<-PATTERN
(send _recv :match _ (int ...))
PATTERN

def_node_matcher :match_operator?, <<-PATTERN
(send !nil? {:=~ :!~} !nil?)
PATTERN
Expand All @@ -109,6 +112,7 @@ def match_with_lvasgn?(node)
MATCH_NODE_PATTERN = <<-PATTERN
{
#match_method?
#match_with_int_arg_method?
#match_operator?
#match_threequals?
#match_with_lvasgn?
Expand Down Expand Up @@ -143,7 +147,7 @@ def on_case(node)

def autocorrect(node)
lambda do |corrector|
if match_method?(node)
if match_method?(node) || match_with_int_arg_method?(node)
corrector.replace(node.loc.selector, 'match?')
elsif match_operator?(node) || match_threequals?(node)
recv, oper, arg = *node
Expand Down