Sha256: 940fe844d895d50e753646cb586532dcee73d44589edbb28e9ce26ae9f73cb34

Contents?: true

Size: 1.65 KB

Versions: 12

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop identifies unnecessary use of a regex where
      # `String#include?` would suffice.
      #
      # This cop's offenses are not safe to auto-correct if a receiver is nil.
      #
      # @example
      #   # bad
      #   'abc'.match?(/ab/)
      #   /ab/.match?('abc')
      #   'abc' =~ /ab/
      #   /ab/ =~ 'abc'
      #   'abc'.match(/ab/)
      #   /ab/.match('abc')
      #
      #   # good
      #   'abc'.include?('ab')
      class StringInclude < Base
        extend AutoCorrector

        MSG = 'Use `String#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)
           (match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
        PATTERN

        def on_send(node)
          return unless (receiver, regex_str = redundant_regex?(node))

          add_offense(node) do |corrector|
            receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
            regex_str = interpret_string_escapes(regex_str)

            new_source = "#{receiver.source}.include?(#{to_string_literal(regex_str)})"

            corrector.replace(node.source_range, new_source)
          end
        end
        alias on_match_with_lvasgn on_send

        private

        def literal?(regex_str)
          regex_str.match?(/\A#{Util::LITERAL_REGEX}+\z/o)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-performance-1.11.5 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.11.4 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.11.3 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.11.2 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.11.1 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.11.0 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.10.2 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.10.1 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.10.0 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.9.2 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.9.1 lib/rubocop/cop/performance/string_include.rb
rubocop-performance-1.9.0 lib/rubocop/cop/performance/string_include.rb