Sha256: c9d0df941f2771d2028ddb2b2bcd37d0ea809c534662ed92af2c3d1f40b49f6b

Contents?: true

Size: 917 Bytes

Versions: 9

Compression:

Stored size: 917 Bytes

Contents

# encoding: utf-8

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
      #
      #   sum = numbers.each_with_object(0) { |e, a| a += e }
      class EachWithObjectArgument < Cop
        MSG = 'The argument to each_with_object can not be immutable.'

        def on_send(node)
          _receiver, method_name, *args = *node
          return unless method_name == :each_with_object
          return unless args.length == 1

          arg = args.first
          add_offense(node, :expression) if arg.int_type? || arg.float_type?
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.35.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.34.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.34.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.34.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.33.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.32.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.32.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.31.0 lib/rubocop/cop/lint/each_with_object_argument.rb