Sha256: 9cfda31301f319b4361ae8a5dda6d4675e725dc613e0ca4e59933513172bd890

Contents?: true

Size: 921 Bytes

Versions: 30

Compression:

Stored size: 921 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 < Base
        extend AutoCorrector
        extend TargetRubyVersion

        minimum_target_ruby_version 2.6

        MSG = 'Prefer ary[n..] over ary[n..-1].'
        RESTRICT_ON_SEND = %i[[]].freeze

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

        def on_send(node)
          return unless node.arguments.count == 1
          return unless range_till_minus_one?(node.arguments.first)

          add_offense(node.first_argument) do |corrector|
            corrector.remove(node.first_argument.end)
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.10.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.9.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.9.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.8.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.8.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.7.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.6.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.6.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.5.2 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.5.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.5.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.4.2 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.4.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.4.0 lib/rubocop/cop/style/slicing_with_range.rb