Sha256: 4767444a8019496e9676e3142abd58b26ec9e7d1a71f8e14871525f0afdfda60

Contents?: true

Size: 1.99 KB

Versions: 157

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for the instantiation of array using redundant `Array` constructor.
      # Autocorrect replaces to array literal which is the simplest and fastest.
      #
      # @example
      #
      #   # bad
      #   Array.new([])
      #   Array[]
      #   Array([])
      #   Array.new(['foo', 'foo', 'foo'])
      #   Array['foo', 'foo', 'foo']
      #   Array(['foo', 'foo', 'foo'])
      #
      #   # good
      #   []
      #   ['foo', 'foo', 'foo']
      #   Array.new(3, 'foo')
      #   Array.new(3) { 'foo' }
      #
      class RedundantArrayConstructor < Base
        extend AutoCorrector

        MSG = 'Remove the redundant `Array` constructor.'

        RESTRICT_ON_SEND = %i[new [] Array].freeze

        # @!method redundant_array_constructor(node)
        def_node_matcher :redundant_array_constructor, <<~PATTERN
          {
            (send
              (const {nil? cbase} :Array) :new
              $(array ...))
            (send
              (const {nil? cbase} :Array) :[]
              $...)
            (send
              nil? :Array
              $(array ...))
          }
        PATTERN

        def on_send(node)
          return unless (array_literal = redundant_array_constructor(node))

          receiver = node.receiver
          selector = node.loc.selector

          if node.method?(:new)
            range = receiver.source_range.join(selector)
            replacement = array_literal
          elsif node.method?(:Array)
            range = selector
            replacement = array_literal
          else
            range = receiver
            replacement = selector.begin.join(node.source_range.end)
          end

          register_offense(range, node, replacement)
        end

        private

        def register_offense(range, node, replacement)
          add_offense(range) do |corrector|
            corrector.replace(node, replacement.source)
          end
        end
      end
    end
  end
end

Version data entries

157 entries across 156 versions & 16 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.73.2 lib/rubocop/cop/style/redundant_array_constructor.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.73.1 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.73.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.72.2 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.72.1 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.72.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.71.2 lib/rubocop/cop/style/redundant_array_constructor.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.71.1 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.71.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.70.0 lib/rubocop/cop/style/redundant_array_constructor.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.69.2 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.69.1 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.69.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.68.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.67.0 lib/rubocop/cop/style/redundant_array_constructor.rb
rubocop-1.66.1 lib/rubocop/cop/style/redundant_array_constructor.rb