Class: RuboCop::Cop::Style::NegativeArrayIndex

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/negative_array_index.rb

Overview

Identifies usages of ‘arr[arr.length - n]`, `arr[arr.size - n]`, or `arr[arr.count - n]` and suggests to change them to use arr[-n] instead. Also handles range patterns like `arr[0..(arr.length - n)]`.

The cop recognizes preserving methods (sort, reverse, shuffle, rotate) and their combinations, allowing safe replacement when the receiver matches. It works with variables, instance variables, class variables, and constants.

Examples:

# bad
arr[arr.count - 2]
arr[0..(arr.length - 2)]
arr[0...(arr.length - 4)]
arr.sort[arr.reverse.length - 2]
arr.sort.reverse[arr.sort.size - 2]

# good
arr[-2]
arr[0..-2]
arr[0...-4]
arr.sort[-2]
arr.sort.reverse[-2]

Constant Summary collapse

MSG =
'Use `%<receiver>s[-%<index>s]` instead of `%<current>s`.'
MSG_RANGE =
'Use `%<receiver>s[%<start>s%<range_op>s-%<index>s]` instead of `%<current>s`.'
RESTRICT_ON_SEND =
i[[]].freeze
LENGTH_METHODS =
i[length size count].freeze
PRESERVING_METHODS =
i[sort reverse shuffle rotate].freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#length_subtraction?(node) ⇒ Object



42
43
44
45
46
# File 'lib/rubocop/cop/style/negative_array_index.rb', line 42

def_node_matcher :length_subtraction?, "(send\n  (send $_ {:length :size :count}) :-\n  (int $_))\n"

#on_send(node) ⇒ Object Also known as: on_csend



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/style/negative_array_index.rb', line 48

def on_send(node)
  return if node.arguments.empty?

  index_arg = node.first_argument
  range_node = extract_range_from_begin(index_arg)
  if range_with_length_subtraction?(range_node, node.receiver)
    receiver = node.receiver.source
    return handle_range_pattern(receiver, range_node, index_arg)
  end

  handle_simple_index_pattern(node, index_arg)
end