Sha256: ac30c1bbdff7c800f7c314db96a0731832c5b9788db8da103410723ac7d8e43e

Contents?: true

Size: 1.85 KB

Versions: 24

Compression:

Stored size: 1.85 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 < Cop
        include RangeHelp

        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)
          redundant_with_object?(node) do |send|
            add_offense(node, location: with_object_range(send))
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            redundant_with_object?(node) do |send|
              if send.method?(:each_with_object)
                corrector.replace(with_object_range(send), 'each')
              else
                corrector.remove(with_object_range(send))
                corrector.remove(send.loc.dot)
              end
            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

24 entries across 23 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.88.0 lib/rubocop/cop/lint/redundant_with_object.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.87.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.87.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.86.0 lib/rubocop/cop/lint/redundant_with_object.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/redundant_with_object.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/lint/redundant_with_object.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.85.1 lib/rubocop/cop/lint/redundant_with_object.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.85.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.84.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.83.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.82.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.81.0 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.80.1 lib/rubocop/cop/lint/redundant_with_object.rb
rubocop-0.80.0 lib/rubocop/cop/lint/redundant_with_object.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/redundant_with_object.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/redundant_with_object.rb