Sha256: 3fc6f17309684615e10fe39c1b2a94cbaa0baad1eff23167c8fcc9ad2d71e36a

Contents?: true

Size: 1.04 KB

Versions: 28

Compression:

Stored size: 1.04 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|
            return unless arg.immutable_literal?

            add_offense(node)
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 26 versions & 3 rubygems

Version Path
rubocop-0.63.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.63.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.62.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.61.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.61.0 lib/rubocop/cop/lint/each_with_object_argument.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/each_with_object_argument.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/each_with_object_argument.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/each_with_object_argument.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.60.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.59.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.59.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.59.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.58.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.58.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.58.0 lib/rubocop/cop/lint/each_with_object_argument.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.57.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.57.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.57.0 lib/rubocop/cop/lint/each_with_object_argument.rb