class RuboCop::Cop::Lint::NextWithoutAccumulator

Don’t omit the accumulator when calling ‘next` in a `reduce` block.

@example

# bad

result = (1..4).reduce(0) do |acc, i|
  next if i.odd?
  acc + i
end

@example

# good

result = (1..4).reduce(0) do |acc, i|
  next acc if i.odd?
  acc + i
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/lint/next_without_accumulator.rb, line 28
def on_block(node)
  on_block_body_of_reduce(node) do |body|
    void_next = body.each_node(:next).find do |n|
      n.children.empty? && parent_block_node(n) == node
    end

    add_offense(void_next) if void_next
  end
end
on_numblock(node) click to toggle source
# File lib/rubocop/cop/lint/next_without_accumulator.rb, line 38
def on_numblock(node)
  on_numblock_body_of_reduce(node) do |body|
    void_next = body.each_node(:next).find do |n|
      n.children.empty? && parent_numblock_node(n) == node
    end

    add_offense(void_next) if void_next
  end
end

Private Instance Methods

parent_block_node(node) click to toggle source
# File lib/rubocop/cop/lint/next_without_accumulator.rb, line 60
def parent_block_node(node)
  node.each_ancestor(:block).first
end
parent_numblock_node(node) click to toggle source
# File lib/rubocop/cop/lint/next_without_accumulator.rb, line 64
def parent_numblock_node(node)
  node.each_ancestor(:numblock).first
end