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#5569.

This PR fixes a false negative for `Performance/RegexpMatch`
when `MatchData` is not detected in `if` branch of guard condition.

```console
% cat example.rb
# frozen_string_literal: true

raise unless ex.message =~ /can't modify frozen IOError/

% bundle exec rubocop --only Performance/RegexpMatch
Inspecting 1 file
.

1 file inspected, no offenses detected
```

This issue was detected by the following code.
https://github.com/rails/rails/blob/v6.0.0.rc2/actioncable/test/client_test.rb#L94
  • Loading branch information
koic committed Jul 27, 2019
1 parent c761793 commit d8026ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-07-20 09:00:25 +0900 using RuboCop version 0.72.0.
# on 2019-07-27 17:51:55 +0800 using RuboCop version 0.73.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -15,16 +15,16 @@ InternalAffairs/NodeDestructuring:
- 'lib/rubocop/cop/performance/regexp_match.rb'
- 'lib/rubocop/cop/performance/string_replacement.rb'

# Offense count: 7
# Offense count: 8
Metrics/AbcSize:
Max: 17

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 149
Max: 156

# Offense count: 14
# Offense count: 15
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 14
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#67](https://github.com/rubocop-hq/rubocop-performance/issues/67): Fix an error for `Performance/RedundantMerge` when `MaxKeyValuePairs` option is set to `null`. ([@koic][])
* [#73](https://github.com/rubocop-hq/rubocop-performance/pull/73): Fix a false negative for `Performance/RegexpMatch` when `MatchData` is not detected in `if` branch of guard condition. ([@koic][])

## 1.4.0 (2019-06-20)

Expand Down
20 changes: 14 additions & 6 deletions lib/rubocop/cop/performance/regexp_match.rb
Expand Up @@ -177,15 +177,23 @@ def last_match_used?(match_node)
scope_root = scope_root(match_node)
body = scope_root ? scope_body(scope_root) : match_node.ancestors.last

return true if match_node.parent.if_type? &&
match_node.parent.modifier_form?
range =
if match_node.parent.if_type? && match_node.parent.modifier_form?
if_branch_range(match_node)
else
match_node_pos = match_node.loc.expression.begin_pos
next_match_pos = next_match_pos(body, match_node_pos, scope_root)

match_node_pos..next_match_pos
end

match_node_pos = match_node.loc.expression.begin_pos
find_last_match(body, range, scope_root)
end

next_match_pos = next_match_pos(body, match_node_pos, scope_root)
range = match_node_pos..next_match_pos
def if_branch_range(match_node)
expression = match_node.parent.if_branch.loc.expression

find_last_match(body, range, scope_root)
expression.begin_pos..expression.end_pos
end

def next_match_pos(body, match_node_pos, scope_root)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/performance/regexp_match_spec.rb
Expand Up @@ -38,6 +38,18 @@
end
RUBY2

include_examples 'offense', "#{name} in if condition", <<-RUBY, <<-RUBY2
do_something if #{cond}
RUBY
do_something if #{correction}
RUBY2

include_examples 'offense', "#{name} in unless condition", <<-RUBY, <<-RUBY2
do_something unless #{cond}
RUBY
do_something unless #{correction}
RUBY2

include_examples 'offense', "#{name} in elsif condition", <<-RUBY, <<-RUBY2
if cond
do_something
Expand Down

0 comments on commit d8026ba

Please sign in to comment.