Sha256: ae2efa63f1a8b1cb8383210c2fd267d6cdd87a7a26987572e5f4f87b02918623

Contents?: true

Size: 1.1 KB

Versions: 17

Compression:

Stored size: 1.1 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.
      #
      # This cop is marked as unsafe. But probably it's quite unlikely that some other class would
      # define a `split` method that takes exactly the same arguments.
      #
      # @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

17 entries across 17 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.20.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.19.1 lib/rubocop/cop/style/string_chars.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/string_chars.rb
rubocop-1.19.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.18.4 lib/rubocop/cop/style/string_chars.rb
rubocop-1.18.3 lib/rubocop/cop/style/string_chars.rb
rubocop-1.18.2 lib/rubocop/cop/style/string_chars.rb
rubocop-1.18.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.18.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.17.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.16.1 lib/rubocop/cop/style/string_chars.rb
rubocop-1.16.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.15.0 lib/rubocop/cop/style/string_chars.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/string_chars.rb
rubocop-1.14.0 lib/rubocop/cop/style/string_chars.rb
rubocop-1.13.0 lib/rubocop/cop/style/string_chars.rb