Sha256: e34474d252d7f547bc8810cd1cd221f770d076e7d405661cb0323357e6766145

Contents?: true

Size: 1.97 KB

Versions: 74

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  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 or a Symbol.
      #
      # @example
      #   # bad
      #   str.match?(/ab/)
      #   /ab/.match?(str)
      #   str =~ /ab/
      #   /ab/ =~ str
      #   str.match(/ab/)
      #   /ab/.match(str)
      #
      #   # good
      #   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
          {(call $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
           (send (regexp (str $#literal?) (regopt)) {:match :match?} $_)
           (match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
        PATTERN

        # rubocop:disable Metrics/AbcSize
        def on_send(node)
          return unless (receiver, regex_str = redundant_regex?(node))

          negation = node.send_type? && node.method?(:!~)
          message = format(MSG, negation: ('!' if negation))

          add_offense(node, message: message) do |corrector|
            receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
            regex_str = interpret_string_escapes(regex_str)
            dot = node.loc.dot ? node.loc.dot.source : '.'

            new_source = "#{'!' if negation}#{receiver.source}#{dot}include?(#{to_string_literal(regex_str)})"

            corrector.replace(node, new_source)
          end
        end
        # rubocop:enable Metrics/AbcSize
        alias on_csend on_send
        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

74 entries across 74 versions & 2 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/string_include.rb