Sha256: 19b08f8ef47e6b7b9db856de1e3dd433507d5033e5051af8340d4b58243a11d6

Contents?: true

Size: 1.2 KB

Versions: 38

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for uses of `String#split` with empty string or regexp literal argument.
      #
      # @safety
      #   This cop is unsafe because it cannot be guaranteed that the receiver
      #   is actually a string. If another class has a `split` method with
      #   different behavior, it would be registered as a false positive.
      #
      # @example
      #   # bad
      #   string.split(//)
      #   string.split('')
      #
      #   # good
      #   string.chars
      #
      class StringChars < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `chars` instead of `%<current>s`.'
        RESTRICT_ON_SEND = %i[split].freeze
        BAD_ARGUMENTS = %w[// '' ""].freeze

        def on_send(node)
          return unless node.arguments.one? && BAD_ARGUMENTS.include?(node.first_argument.source)

          range = range_between(node.loc.selector.begin_pos, node.source_range.end_pos)

          add_offense(range, message: format(MSG, current: range.source)) do |corrector|
            corrector.replace(range, 'chars')
          end
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 8 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.70.0 lib/rubocop/cop/style/string_chars.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_chars.rb
rubocop-1.69.2 lib/rubocop/cop/style/string_chars.rb
rubocop-1.69.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.69.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.68.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.67.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.66.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.66.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.65.1 lib/rubocop/cop/style/string_chars.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_chars.rb
rubocop-1.65.0 lib/rubocop/cop/style/string_chars.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/string_chars.rb
rubocop-1.64.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.63.4 lib/rubocop/cop/style/string_chars.rb
rubocop-1.63.3 lib/rubocop/cop/style/string_chars.rb
rubocop-1.63.2 lib/rubocop/cop/style/string_chars.rb
rubocop-1.63.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.63.0 lib/rubocop/cop/style/string_chars.rb