Sha256: f7945f343159c0dc6e0202334bd2075f7953663255f3bf36b1a2d53a62a0c0bd
Contents?: true
Size: 1.15 KB
Versions: 3
Compression:
Stored size: 1.15 KB
Contents
# encoding: utf-8 # frozen_string_literal: true module RuboCop module Cop module Lint # 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 class NextWithoutAccumulator < Cop MSG = 'Use `next` with an accumulator argument in a `reduce`.'.freeze def_node_matcher :on_body_of_reduce, <<-PATTERN (block (send _recv {:reduce :inject} !sym) _blockargs $(begin ...)) PATTERN def on_block(node) on_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, :expression) if void_next end end private def parent_block_node(node) node.each_ancestor.find { |n| n.type == :block } end end end end end
Version data entries
3 entries across 3 versions & 2 rubygems