Sha256: e8ef2768341abdf8f1c520dcd2015e556857d779af3caceab37313347dffd194

Contents?: true

Size: 1.36 KB

Versions: 28

Compression:

Stored size: 1.36 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`.'

        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
        alias on_numblock on_block

        private

        # @!method on_block_body_of_reduce(node)
        def_node_matcher :on_block_body_of_reduce, <<~PATTERN
          {
            (block (call _recv {:reduce :inject} !sym) _blockargs $(begin ...))
            (numblock (call _recv {:reduce :inject} !sym) _argscount $(begin ...))
          }
        PATTERN

        def parent_block_node(node)
          node.each_ancestor(:block, :numblock).first
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 8 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/next_without_accumulator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.65.0 lib/rubocop/cop/lint/next_without_accumulator.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.64.1 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.63.4 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.63.3 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.63.2 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.63.1 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.63.0 lib/rubocop/cop/lint/next_without_accumulator.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.62.1 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.62.0 lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.61.0 lib/rubocop/cop/lint/next_without_accumulator.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/next_without_accumulator.rb
rubocop-1.60.2 lib/rubocop/cop/lint/next_without_accumulator.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/next_without_accumulator.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/next_without_accumulator.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/next_without_accumulator.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/lint/next_without_accumulator.rb