Sha256: e411f3f25835ffbd5843c4ba231bfcfc455477fa09bfb54e1a3bcfb9a6ddaa27

Contents?: true

Size: 919 Bytes

Versions: 1

Compression:

Stored size: 919 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks that arrays are sliced with endless ranges instead of
      # `ary[start..-1]` on Ruby 2.6+.
      #
      # @example
      #   # bad
      #   items[1..-1]
      #
      #   # good
      #   items[1..]
      class SlicingWithRange < Cop
        extend TargetRubyVersion

        minimum_target_ruby_version 2.6

        MSG = 'Prefer ary[n..] over ary[n..-1].'

        def_node_matcher :range_till_minus_one?, '(irange (int _) (int -1))'

        def on_send(node)
          return unless node.method?(:[]) && node.arguments.count == 1
          return unless range_till_minus_one?(node.arguments.first)

          add_offense(node.arguments.first)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.remove(node.end)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.83.0 lib/rubocop/cop/style/slicing_with_range.rb