Sha256: 5fa54a38571601b5ae25493adf974b39a947ab3d401604504fef726b42ea1297

Contents?: true

Size: 968 Bytes

Versions: 20

Compression:

Stored size: 968 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

        # @!method range_till_minus_one?(node)
        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

20 entries across 20 versions & 3 rubygems

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