Skip to content

Commit

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

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

```ruby
# example.rb
def foo
  if /re/.match(foo, 1)
    do_something
  end
end
```

## RuboCop 0.67

RuboCop 0.67 warns.

```console
% rubocop _0.67.2_ --only Performance/RegexpMatch
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
       ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

## RuboCop 0.68 (Before)

RuboCop 0.68 does not warn. This is a false negative.

```console
% rubocop _0.68.1_ --only Performance/RegexpMatch --require
rubocop-performance
Inspecting 1 file
.

1 file inspected, no offenses detected
```

## RuboCop 0.68 (After)

This PR fixes a false negative.

```console
% rubocop --only Performance/RegexpMatch --require rubocop-performance
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
       ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

This fix uses the following `<>` node pattern notation introduced in
RuboCop 0.68, so it supports Ruby version 0.68 or higher.
rubocop/rubocop#6965
  • Loading branch information
koic committed May 3, 2019
1 parent a10ccf8 commit 25e8894
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#47](https://github.com/rubocop-hq/rubocop-performance/pull/47): Fix a false negartive for `Performance/RegexpMatch` when using RuboCop 0.68 or higher. ([@koic][])

## 1.1.0 (2019-04-08)

### Changes
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/regexp_match.rb
Expand Up @@ -86,8 +86,8 @@ class RegexpMatch < Cop

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

Expand Down
2 changes: 1 addition & 1 deletion rubocop-performance.gemspec
Expand Up @@ -29,6 +29,6 @@ Gem::Specification.new do |s|
'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop-performance/issues'
}

s.add_runtime_dependency('rubocop', '>= 0.67.0')
s.add_runtime_dependency('rubocop', '>= 0.68.0')
s.add_development_dependency('simplecov')
end

0 comments on commit 25e8894

Please sign in to comment.