Skip to content

Commit

Permalink
Fix a false negative for Performance/RegexpMatch
Browse files Browse the repository at this point in the history
Follow up rubocop/rubocop#7081.

This PR fixes a false negartive for `Performance/RegexpMatch` when using
RuboCop 0.71 or higher.

This causes CI to fail.
https://circleci.com/gh/rubocop-hq/rubocop-performance/588

This is a regression similar to rubocop#47. This PR tries to fix it using
a node pattern that has been used for a long time.
  • Loading branch information
koic committed Jun 6, 2019
1 parent 4757b28 commit f1e963e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
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

0 comments on commit f1e963e

Please sign in to comment.