Sha256: c63dc4376c40edcf08fec8ea0e159b205e9a053de701676e27c25339eeec2b39

Contents?: true

Size: 1.74 KB

Versions: 34

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop 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_node_matcher :redundant_with_object?, <<~PATTERN
          (block
            $(send _ {:each_with_object :with_object}
              _)
            (args
              (arg _))
            ...)
        PATTERN

        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

        private

        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.loc.expression.end_pos
          )
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.10.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.9.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.9.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.8.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.8.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.7.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.6.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.6.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.5.2 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.5.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.5.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.4.2 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.4.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-1.4.0 lib/rubocop/cop/lint/redundant_with_object.rb