Sha256: 58f2bc2002e5850b11a47850a3a901d64032158ac0c1959df9b3b2ae9765e98e

Contents?: true

Size: 1.33 KB

Versions: 34

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks that arrays are sliced with endless ranges instead of
      # `ary[start..-1]` on Ruby 2.6+.
      #
      # @safety
      #   This cop is unsafe because `x..-1` and `x..` are only guaranteed to
      #   be equivalent for `Array#[]`, and the cop cannot determine what class
      #   the receiver is.
      #
      #   For example:
      #   [source,ruby]
      #   ----
      #   sum = proc { |ary| ary.sum }
      #   sum[-3..-1] # => -6
      #   sum[-3..] # Hangs forever
      #   ----
      #
      # @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

34 entries across 30 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/slicing_with_range.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/slicing_with_range.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/slicing_with_range.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.45.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.45.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.44.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.44.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.43.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.42.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.41.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.41.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.40.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.39.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-1.38.0 lib/rubocop/cop/style/slicing_with_range.rb