From 10d1b9450ac6fc2da3867901089fb844d89349ee Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 7 Jun 2022 19:28:57 +0900 Subject: [PATCH] Fix a false positive for `Performance/RegexpMatch` Follow up https://github.com/rubocop/rubocop-performance/pull/288#issuecomment-1148453972. This PR fixes a false positive for `Performance/RegexpMatch` when `TargetRubyVersion: 2.3`. --- .../fix_a_false_positive_for_performance_regexp_match.md | 1 + lib/rubocop/cop/performance/regexp_match.rb | 3 +++ spec/rubocop/cop/performance/regexp_match_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_performance_regexp_match.md diff --git a/changelog/fix_a_false_positive_for_performance_regexp_match.md b/changelog/fix_a_false_positive_for_performance_regexp_match.md new file mode 100644 index 0000000000..b766849448 --- /dev/null +++ b/changelog/fix_a_false_positive_for_performance_regexp_match.md @@ -0,0 +1 @@ +* [#292](https://github.com/rubocop/rubocop-performance/pull/292): Fix a false positive for `Performance/RegexpMatch` when `TargetRubyVersion: 2.3`. ([@koic][]) diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index e723a5e970..2b7eecc7e8 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -74,6 +74,9 @@ module Performance # end class RegexpMatch < Base extend AutoCorrector + extend TargetRubyVersion + + minimum_target_ruby_version 2.4 # Constants are included in this list because it is unlikely that # someone will store `nil` as a constant and then use it for comparison diff --git a/spec/rubocop/cop/performance/regexp_match_spec.rb b/spec/rubocop/cop/performance/regexp_match_spec.rb index d8ac6f5d82..a852762f4f 100644 --- a/spec/rubocop/cop/performance/regexp_match_spec.rb +++ b/spec/rubocop/cop/performance/regexp_match_spec.rb @@ -423,4 +423,13 @@ def foo end RUBY end + + context 'when Ruby <= 2.3', :ruby23 do + it 'does not register an offense when using `String#match` in condition' do + expect_no_offenses(<<~RUBY) + if 'foo'.match(re) + end + RUBY + end + end end