Sha256: 4a81ef613dfd93ab4e7d5e0035faf47e8d7bd06708d841d59b6150e12932369c
Contents?: true
Size: 1.19 KB
Versions: 52
Compression:
Stored size: 1.19 KB
Contents
# 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 # # @example # # # good # # result = (1..4).reduce(0) do |acc, i| # next acc if i.odd? # acc + i # end class NextWithoutAccumulator < Base MSG = 'Use `next` with an accumulator argument in a `reduce`.' # @!method on_body_of_reduce(node) 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) if void_next end end private def parent_block_node(node) node.each_ancestor(:block).first end end end end end
Version data entries
52 entries across 50 versions & 6 rubygems