From 25e889420e2c7ad675f0128305ccc2c27d9673a9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 2 May 2019 14:34:17 -0500 Subject: [PATCH] Fix a false negartive for `Performance/RegexpMatch` Follow up https://github.com/rubocop-hq/rubocop/pull/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. https://github.com/rubocop-hq/rubocop/pull/6965 --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/performance/regexp_match.rb | 2 +- rubocop-performance.gemspec | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d54bb08536..37d4414e76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index c98d9a715a..000485a039 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -86,8 +86,8 @@ class RegexpMatch < Cop def_node_matcher :match_method?, <<-PATTERN { + (send _recv :match _ ) (send _recv :match _) - (send _recv :match _ (int ...)) } PATTERN diff --git a/rubocop-performance.gemspec b/rubocop-performance.gemspec index bbc5fe160a..d61676459e 100644 --- a/rubocop-performance.gemspec +++ b/rubocop-performance.gemspec @@ -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