class RuboCop::Cop::Performance::ChainArrayAllocation

array
array.map! { |x| x.downcase }
array.flatten!
array.compact!
array = [“a”, “b”, “c”]
# good
array.compact.flatten.map { |x| x.downcase }
array = [“a”, “b”, “c”]
# bad
@example
that is promptly thrown away. Instead it is faster to mutate when we know it’s safe.
Each of these methods (‘compact`, `flatten`, `map`) will generate a new intermediate array
Identifies usages of `array.compact.flatten.map { |x| x.downcase }`.

def enumerable_select_method?(node)

def enumerable_select_method?(node)
  # NOTE: `QueryMethods#select` in Rails accepts positional arguments, whereas `Enumerable#select` does not.
  #        This difference can be utilized to reduce the knowledge requirements related to `select`.
  (node.block_type? || node.numblock_type?) && node.send_node.arguments.empty?
end

def on_send(node)

def on_send(node)
  chain_array_allocation?(node) do |fm, sm|
    return if node.each_descendant(:send).any? { |descendant| descendant.method?(:lazy) }
    return if node.method?(:select) && !enumerable_select_method?(node.receiver)
    range = node.loc.selector.begin.join(node.source_range.end)
    add_offense(range, message: format(MSG, method: fm, second_method: sm))
  end
end