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

# 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 27
def on_block(node)
  on_body_of_reduce(node) do |body|
    void_next = body.each_node(:next).find { |n| n.children.empty? }

    add_offense(void_next, :expression) if void_next
  end
end