From 2842d2be0d40e9b1278c2457188b9a2162b06782 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 1 Dec 2022 22:54:59 +0200 Subject: [PATCH] Fix `Performance/RegexpMatch` to handle `::Regexp` --- changelog/fix_regexp_match_to_handle_cbase_regexp.md | 1 + lib/rubocop/cop/performance/regexp_match.rb | 4 ++-- spec/rubocop/cop/performance/regexp_match_spec.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_regexp_match_to_handle_cbase_regexp.md diff --git a/changelog/fix_regexp_match_to_handle_cbase_regexp.md b/changelog/fix_regexp_match_to_handle_cbase_regexp.md new file mode 100644 index 0000000000..c1e62741fe --- /dev/null +++ b/changelog/fix_regexp_match_to_handle_cbase_regexp.md @@ -0,0 +1 @@ +* [#314](https://github.com/rubocop/rubocop-performance/issues/314): Fix `Performance/RegexpMatch` to handle `::Regexp`. ([@fatkodima][]) diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index 5a5c5ee62c..43ba6e74fe 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -124,8 +124,8 @@ def match_with_lvasgn?(node) def_node_search :last_matches, <<~PATTERN { - (send (const nil? :Regexp) :last_match) - (send (const nil? :Regexp) :last_match _) + (send (const {nil? cbase} :Regexp) :last_match) + (send (const {nil? cbase} :Regexp) :last_match _) ({back_ref nth_ref} _) (gvar #match_gvar?) } diff --git a/spec/rubocop/cop/performance/regexp_match_spec.rb b/spec/rubocop/cop/performance/regexp_match_spec.rb index 7af8e25800..08fc0dd477 100644 --- a/spec/rubocop/cop/performance/regexp_match_spec.rb +++ b/spec/rubocop/cop/performance/regexp_match_spec.rb @@ -98,7 +98,7 @@ def foo %w[ $& $' $` $~ $1 $2 $100 $MATCH - Regexp.last_match Regexp.last_match(1) + Regexp.last_match ::Regexp.last_match Regexp.last_match(1) ].each do |var| it "accepts #{name} in method with #{var}" do expect_no_offenses(<<~RUBY)