From 357f6dc870fd9bfa03ce381ad154f1064e34399e Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sat, 7 Jan 2023 17:06:36 +0200 Subject: [PATCH] Register offenses for variables against regexes in `Performance/StringInclude` --- ..._register_offenses_for_variables_against.md | 1 + lib/rubocop/cop/performance/string_include.rb | 18 +++++++++--------- .../cop/performance/string_include_spec.rb | 10 +++++----- 3 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 changelog/change_register_offenses_for_variables_against.md diff --git a/changelog/change_register_offenses_for_variables_against.md b/changelog/change_register_offenses_for_variables_against.md new file mode 100644 index 0000000000..3540432159 --- /dev/null +++ b/changelog/change_register_offenses_for_variables_against.md @@ -0,0 +1 @@ +* [#332](https://github.com/rubocop/rubocop-performance/pull/332): Register offenses for variables against regexes in `Performance/StringInclude`. ([@fatkodima][]) diff --git a/lib/rubocop/cop/performance/string_include.rb b/lib/rubocop/cop/performance/string_include.rb index 162708c172..c605a3b39d 100644 --- a/lib/rubocop/cop/performance/string_include.rb +++ b/lib/rubocop/cop/performance/string_include.rb @@ -6,19 +6,19 @@ module Performance # Identifies unnecessary use of a regex where `String#include?` would suffice. # # @safety - # This cop's offenses are not safe to autocorrect if a receiver is nil. + # This cop's offenses are not safe to autocorrect if a receiver is nil or a Symbol. # # @example # # bad - # 'abc'.match?(/ab/) - # /ab/.match?('abc') - # 'abc' =~ /ab/ - # /ab/ =~ 'abc' - # 'abc'.match(/ab/) - # /ab/.match('abc') + # str.match?(/ab/) + # /ab/.match?(str) + # str =~ /ab/ + # /ab/ =~ str + # str.match(/ab/) + # /ab/.match(str) # # # good - # 'abc'.include?('ab') + # str.include?('ab') class StringInclude < Base extend AutoCorrector @@ -27,7 +27,7 @@ class StringInclude < Base def_node_matcher :redundant_regex?, <<~PATTERN {(send $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt))) - (send (regexp (str $#literal?) (regopt)) {:match :match?} $str) + (send (regexp (str $#literal?) (regopt)) {:match :match?} $_) (match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)} PATTERN diff --git a/spec/rubocop/cop/performance/string_include_spec.rb b/spec/rubocop/cop/performance/string_include_spec.rb index f3c5946452..df02a284a0 100644 --- a/spec/rubocop/cop/performance/string_include_spec.rb +++ b/spec/rubocop/cop/performance/string_include_spec.rb @@ -151,11 +151,11 @@ expect_no_offenses('expect(subject.spin).to match(/\A\n/)') end - # Symbol object does not have `include?` method. - # A variable possible to be a symbol object, so if `match?` argument is - # a variable, accept it. - it 'allows argument of `match?` is not a string literal' do - expect_no_offenses('/ /.match?(content_as_symbol)') + it 'registers an offense and corrects when argument of `match?` is not a string literal' do + expect_offense(<<~RUBY) + / /.match?(content) + ^^^^^^^^^^^^^^^^^^^ Use `String#include?` instead of a regex match with literal-only pattern. + RUBY end it 'registers an offense and corrects when using `!~`' do