Sha256: a58788461cddd389a40fac1608d7c3b5ba540d05a9896d5103d0a418951c3c02

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks if each_with_object is called with an immutable
      # argument. Since the argument is the object that the given block shall
      # make calls on to build something based on the enumerable that
      # each_with_object iterates over, an immutable argument makes no sense.
      # It's definitely a bug.
      #
      # @example
      #
      #   # bad
      #
      #   sum = numbers.each_with_object(0) { |e, a| a += e }
      #
      # @example
      #
      #   # good
      #
      #   num = 0
      #   sum = numbers.each_with_object(num) { |e, a| a += e }
      class EachWithObjectArgument < Cop
        MSG = 'The argument to each_with_object can not be immutable.'.freeze

        def_node_matcher :each_with_object?, '(send _ :each_with_object $_)'

        def on_send(node)
          each_with_object?(node) do |arg|
            add_offense(node, :expression) if arg.immutable_literal?
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.47.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.47.0 lib/rubocop/cop/lint/each_with_object_argument.rb