Sha256: 36b31013843f28fa33e1400353c7a264cd9698778587045540232cc76e638818

Contents?: true

Size: 1.18 KB

Versions: 182

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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 < Base
        MSG = 'The argument to each_with_object cannot be immutable.'
        RESTRICT_ON_SEND = %i[each_with_object].freeze

        # @!method each_with_object?(node)
        def_node_matcher :each_with_object?, <<~PATTERN
          (call _ :each_with_object $_)
        PATTERN

        def on_send(node)
          each_with_object?(node) do |arg|
            return unless arg.immutable_literal?

            add_offense(node)
          end
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

182 entries across 175 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/each_with_object_argument.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.65.0 lib/rubocop/cop/lint/each_with_object_argument.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/each_with_object_argument.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/each_with_object_argument.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/each_with_object_argument.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.64.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.63.4 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.63.3 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.63.2 lib/rubocop/cop/lint/each_with_object_argument.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.63.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.63.0 lib/rubocop/cop/lint/each_with_object_argument.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.62.1 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.62.0 lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.61.0 lib/rubocop/cop/lint/each_with_object_argument.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/each_with_object_argument.rb
rubocop-1.60.2 lib/rubocop/cop/lint/each_with_object_argument.rb