Sha256: db9970e944fa3b58b5ae521751fc61310301094983152258ee4c7919cade2fe9

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 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 behaviour, 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
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_chars.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_chars.rb
rubocop-1.28.2 lib/rubocop/cop/style/string_chars.rb
rubocop-1.28.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.28.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.27.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.26.1 lib/rubocop/cop/style/string_chars.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/string_chars.rb
rubocop-1.26.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.25.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.25.0 lib/rubocop/cop/style/string_chars.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/string_chars.rb
rubocop-1.24.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.24.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.23.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.22.3 lib/rubocop/cop/style/string_chars.rb
rubocop-1.22.2 lib/rubocop/cop/style/string_chars.rb
rubocop-1.22.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.22.0 lib/rubocop/cop/style/string_chars.rb