Sha256: 7edda3ec97499954e1875023935c02a18613cbe2b3c5f0ed33c48ff9ed556d25

Contents?: true

Size: 917 Bytes

Versions: 14

Compression:

Stored size: 917 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 !nil? (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

14 entries across 14 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.89.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.88.0 lib/rubocop/cop/style/slicing_with_range.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.87.1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.87.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.86.0 lib/rubocop/cop/style/slicing_with_range.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/slicing_with_range.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/slicing_with_range.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.85.1 lib/rubocop/cop/style/slicing_with_range.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.85.0 lib/rubocop/cop/style/slicing_with_range.rb
rubocop-0.84.0 lib/rubocop/cop/style/slicing_with_range.rb