Sha256: 22b3bafa2914222c83164b95c600313479d4750a87847d02718de82a2578b6a0

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Lint
        # Notice useless overload type parameters.
        #
        # @example default
        #   # bad
        #   def foo: [T] () -> void
        #
        #   # bad
        #   def bar: [T, U] (T) -> void
        #
        #   # good
        #   def foo: [T] (Array[T]) -> T
        class UselessOverloadTypeParams < RuboCop::RBS::CopBase
          MSG = 'Useless overload type variable - `%<variable>s`.'

          def on_rbs_def(decl)
            decl.overloads.each do |overload|
              next if overload.method_type.type_params.empty?

              type_params = overload.method_type.type_params

              overload.method_type.each_type do |type|
                used_variable_in_type(type) do |var|
                  type_params.delete_if { |type_param| type_param.name == var.name }
                end
              end
              next if type_params.empty?

              type_params.each do |type_param|
                next unless type_param.location

                t = location_to_range(type_param.location[:name])
                add_offense(t, message: format(MSG, variable: type_param.name), severity: :warning)
              end
            end
          end

          def used_variable_in_type(type, &block)
            case type
            when ::RBS::Types::Variable
              yield type
            else
              type.each_type do |t|
                used_variable_in_type(t, &block)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-on-rbs-0.2.0 lib/rubocop/cop/rbs/lint/useless_overload_type_params.rb