Class: RuboCop::Cop::RBS::Lint::UnusedOverloadTypeParams

Inherits:
RBS::CopBase
  • Object
show all
Defined in:
lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb

Overview

Notice unused overload type parameters.

Examples:

default

# bad
def foo: [T] () -> void

# good
def foo: [T] (T) -> T

Constant Summary collapse

MSG =
'Unused overload type variable - `%<variable>s`.'

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

#investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#on_rbs_def(decl) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb', line 18

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.dup
    map = type_params.to_h { |param| [param.name, param] }
    type_params.each do |type_param|
      if type_param.upper_bound
        used_variable_in_type(type_param.upper_bound) do |var|
          map.delete(var.name)
        end
      end
    end

    overload.method_type.each_type do |type|
      used_variable_in_type(type) do |var|
        map.delete(var.name)
      end
    end
    next if map.empty?

    map.each do |name, type_param|
      next unless type_param.location

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

#used_variable_in_type(type, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/rbs/lint/unused_overload_type_params.rb', line 48

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