Sha256: 52406286414d883727f3dc29f398a9cf0c5902f4e71055a170b50ae73ac96856

Contents?: true

Size: 992 Bytes

Versions: 10

Compression:

Stored size: 992 Bytes

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 { |n| n.children.empty? }

            add_offense(void_next, :expression) if void_next
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-0.41.2 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.41.1 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.41.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.40.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.39.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.38.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.37.2 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.37.1 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.37.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-0.36.0 lib/rubocop/cop/lint/next_without_accumulator.rb