Sha256: d3ebf632b25b867b957a9721ee40136b8a391b0cdeab0fca07d57fdde125821e

Contents?: true

Size: 1.83 KB

Versions: 32

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for redundant `with_object`.
      #
      # @example
      #   # bad
      #   ary.each_with_object([]) do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      #   # bad
      #   ary.each.with_object([]) do |v|
      #     v
      #   end
      #
      #   # good
      #   ary.each do |v|
      #     v
      #   end
      #
      class RedundantWithObject < Base
        include RangeHelp
        extend AutoCorrector

        MSG_EACH_WITH_OBJECT = 'Use `each` instead of `each_with_object`.'
        MSG_WITH_OBJECT = 'Remove redundant `with_object`.'

        def on_block(node)
          return unless (send = redundant_with_object?(node))

          range = with_object_range(send)

          add_offense(range, message: message(send)) do |corrector|
            if send.method?(:each_with_object)
              corrector.replace(range, 'each')
            else
              corrector.remove(range)
              corrector.remove(send.loc.dot)
            end
          end
        end

        alias on_numblock on_block

        private

        # @!method redundant_with_object?(node)
        def_node_matcher :redundant_with_object?, <<~PATTERN
          {
            (block
              $(call _ {:each_with_object :with_object} _) (args (arg _)) ...)
            (numblock
              $(call _ {:each_with_object :with_object} _) 1 ...)
          }
        PATTERN

        def message(node)
          if node.method?(:each_with_object)
            MSG_EACH_WITH_OBJECT
          else
            MSG_WITH_OBJECT
          end
        end

        def with_object_range(send)
          range_between(send.loc.selector.begin_pos, send.source_range.end_pos)
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 7 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.67.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.66.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.66.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.65.1 lib/rubocop/cop/lint/redundant_with_object.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.65.0 lib/rubocop/cop/lint/redundant_with_object.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.64.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.63.4 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.63.3 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.63.2 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.63.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.63.0 lib/rubocop/cop/lint/redundant_with_object.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.62.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.62.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.61.0 lib/rubocop/cop/lint/redundant_with_object.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.60.2 lib/rubocop/cop/lint/redundant_with_object.rb