Sha256: e0ac21dadd9039b496d968e0671c115d8f275e8ac19315457f1309acb0588da6

Contents?: true

Size: 944 Bytes

Versions: 14

Compression:

Stored size: 944 Bytes

Contents

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

        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.immutable_literal?
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/each_with_object_argument.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.43.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.42.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.41.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.41.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.41.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.40.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.39.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.38.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.37.2 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.37.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.37.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.36.0 lib/rubocop/cop/lint/each_with_object_argument.rb