Sha256: a8d8eb4712fe12a1d31624f8498088de24c36cffde025bfaadcaf76c11ca4832

Contents?: true

Size: 889 Bytes

Versions: 8

Compression:

Stored size: 889 Bytes

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
      #
      #   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_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

8 entries across 8 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-0.46.0 lib/rubocop/cop/lint/each_with_object_argument.rb