class RuboCop::Cop::Style::SlicingWithRange

Checks that arrays are sliced with endless ranges instead of `ary` 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..]

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/slicing_with_range.rb, line 40
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