lib/rubocop/cop/performance/string_include.rb in rubocop-performance-1.15.2 vs lib/rubocop/cop/performance/string_include.rb in rubocop-performance-1.16.0
- old
+ new
@@ -4,31 +4,31 @@
module Cop
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
MSG = 'Use `%<negation>sString#include?` instead of a regex match with literal-only pattern.'
RESTRICT_ON_SEND = %i[match =~ !~ match?].freeze
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
def on_send(node)
return unless (receiver, regex_str = redundant_regex?(node))