typegap 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/LICENSE +21 -0
- data/README.ja.md +65 -0
- data/README.md +93 -0
- data/exe/typegap +6 -0
- data/lib/typegap/cli.rb +70 -0
- data/lib/typegap/finding.rb +38 -0
- data/lib/typegap/project.rb +80 -0
- data/lib/typegap/rules.rb +56 -0
- data/lib/typegap/signature_check.rb +166 -0
- data/lib/typegap/source_check.rb +730 -0
- data/lib/typegap/type_facts.rb +99 -0
- data/lib/typegap/version.rb +6 -0
- data/lib/typegap.rb +39 -0
- data/sig/generated/typegap/cli.rbs +30 -0
- data/sig/generated/typegap/finding.rbs +35 -0
- data/sig/generated/typegap/project.rbs +41 -0
- data/sig/generated/typegap/rules.rbs +36 -0
- data/sig/generated/typegap/signature_check.rbs +60 -0
- data/sig/generated/typegap/source_check.rbs +300 -0
- data/sig/generated/typegap/type_facts.rbs +60 -0
- data/sig/generated/typegap/version.rbs +5 -0
- data/sig/generated/typegap.rbs +14 -0
- metadata +86 -0
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
module Typegap
|
|
5
|
+
# The rules that read one Ruby file's typing. Each rule reports code that
|
|
6
|
+
# the inferred types make unnecessary, impossible, or easy to misuse. In a
|
|
7
|
+
# codebase typed with RBS, most of these are places where the type and the
|
|
8
|
+
# code disagree: either the check is dead, or the type says too much.
|
|
9
|
+
#
|
|
10
|
+
# It reads Steep's typing and the RBS definitions; it does not infer types
|
|
11
|
+
# of its own, and it never changes a file.
|
|
12
|
+
class SourceCheck
|
|
13
|
+
include TypeFacts
|
|
14
|
+
|
|
15
|
+
# RBS core types these as never nil although they return nil (Hash#[] on
|
|
16
|
+
# a missing key, Array#first on an empty array, Thread#join on a
|
|
17
|
+
# timeout). Trusting them would report the very nil checks that keep the
|
|
18
|
+
# code safe.
|
|
19
|
+
NIL_HIDING_CALLS = %i[[] first last join dig find detect max min max_by min_by pop shift sample match fetch_values at].freeze #: Array[Symbol]
|
|
20
|
+
|
|
21
|
+
BASE_TO_S = %w[::Object ::Kernel ::BasicObject].freeze #: Array[String]
|
|
22
|
+
CONVERSIONS = { to_s: "::String", to_str: "::String", to_i: "::Integer", to_int: "::Integer", to_f: "::Float",
|
|
23
|
+
to_sym: "::Symbol", to_a: "::Array", to_ary: "::Array", to_h: "::Hash", to_hash: "::Hash" }.freeze #: Hash[Symbol, String]
|
|
24
|
+
KERNEL_CONVERSIONS = { String: "::String", Integer: "::Integer", Float: "::Float", Array: "::Array", Hash: "::Hash" }.freeze #: Hash[Symbol, String]
|
|
25
|
+
VALUE_CLASSES = %w[::String ::Array ::Hash ::Set].freeze #: Array[String]
|
|
26
|
+
COLLECTIONS = %w[::Array ::Hash ::Set].freeze #: Array[String]
|
|
27
|
+
BLOCKS = %i[block numblock itblock].freeze #: Array[Symbol]
|
|
28
|
+
ITERATORS = %i[each each_with_index each_pair each_key each_value tap].freeze #: Array[Symbol]
|
|
29
|
+
|
|
30
|
+
# @rbs path: String
|
|
31
|
+
# @rbs typing: untyped
|
|
32
|
+
# @rbs builder: untyped
|
|
33
|
+
# @rbs rules: Array[String]
|
|
34
|
+
def initialize(path, typing, builder, rules) #: void
|
|
35
|
+
@path = path
|
|
36
|
+
@typing = typing
|
|
37
|
+
@builder = builder
|
|
38
|
+
@rules = rules
|
|
39
|
+
@found = [] #: Array[Finding]
|
|
40
|
+
@ancestors = {} #: Hash[String, Array[String]?]
|
|
41
|
+
@definitions = {} #: Hash[String, untyped]
|
|
42
|
+
@deciders = {} #: Hash[untyped, Array[untyped]]
|
|
43
|
+
@deciders.compare_by_identity
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
#: () -> Array[Finding]
|
|
47
|
+
def findings
|
|
48
|
+
walk(@typing.source.node, nil, [], false)
|
|
49
|
+
steep_hidden
|
|
50
|
+
@found
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# @rbs node_or_loc: untyped
|
|
56
|
+
# @rbs rule: String
|
|
57
|
+
# @rbs message: String
|
|
58
|
+
#: (untyped, String, String) -> void
|
|
59
|
+
def add(node_or_loc, rule, message)
|
|
60
|
+
return unless @rules.include?(rule)
|
|
61
|
+
|
|
62
|
+
loc = node_or_loc.is_a?(Parser::AST::Node) ? node_or_loc.loc.expression : node_or_loc
|
|
63
|
+
@found << Finding.new(path: @path, line: loc.line, column: loc.column + 1, rule: rule, message: message)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @rbs node: untyped
|
|
67
|
+
#: (untyped) -> String
|
|
68
|
+
def src(node) = node.loc.expression.source.lines.first.chomp
|
|
69
|
+
|
|
70
|
+
# @rbs node: untyped
|
|
71
|
+
# @rbs parent: untyped
|
|
72
|
+
# @rbs nesting: Array[String]
|
|
73
|
+
# @rbs singleton: bool
|
|
74
|
+
#: (untyped, untyped, Array[String], bool) -> void
|
|
75
|
+
def walk(node, parent, nesting, singleton)
|
|
76
|
+
return unless node.is_a?(Parser::AST::Node)
|
|
77
|
+
|
|
78
|
+
check(node, parent, nesting, singleton)
|
|
79
|
+
case node.type
|
|
80
|
+
when :class, :module
|
|
81
|
+
inner = nesting + [node.children[0].loc.expression.source.delete_prefix("::")]
|
|
82
|
+
node.children.drop(1).each { walk(_1, node, inner, false) }
|
|
83
|
+
when :sclass
|
|
84
|
+
node.children.drop(1).each { walk(_1, node, nesting, true) }
|
|
85
|
+
else
|
|
86
|
+
node.children.each { walk(_1, node, nesting, singleton) }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @rbs node: untyped
|
|
91
|
+
# @rbs parent: untyped
|
|
92
|
+
# @rbs nesting: Array[String]
|
|
93
|
+
# @rbs singleton: bool
|
|
94
|
+
#: (untyped, untyped, Array[String], bool) -> void
|
|
95
|
+
def check(node, parent, nesting, singleton)
|
|
96
|
+
case node.type
|
|
97
|
+
when :csend then safe_navigation(node)
|
|
98
|
+
when :send then send_rules(node, parent)
|
|
99
|
+
when :if
|
|
100
|
+
condition(node.children[0], :condition)
|
|
101
|
+
node.children.drop(1).compact.each { @deciders[_1] = [node.children[0]] }
|
|
102
|
+
when :while, :until then condition(node.children[0], :condition)
|
|
103
|
+
when :and then condition(node.children[0], :and)
|
|
104
|
+
when :or then or_rules(node)
|
|
105
|
+
when :dstr, :dsym then interpolation(node)
|
|
106
|
+
when :case
|
|
107
|
+
case_rules(node)
|
|
108
|
+
node.children.drop(1).compact.each { @deciders[_1] = [node.children[0], *_1.children] }
|
|
109
|
+
when *BLOCKS then block_rules(node, parent)
|
|
110
|
+
# rbs-inline reads `C = v #: T` to write the constant's type, so that
|
|
111
|
+
# assertion is needed even when Steep infers the same type.
|
|
112
|
+
when :assertion then assertion(node) unless parent&.type == :casgn
|
|
113
|
+
when :splat then splat(node)
|
|
114
|
+
when :def then def_rules(node, nesting, singleton)
|
|
115
|
+
when :defs then def_rules(node, nesting, true)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# --- types ---
|
|
120
|
+
|
|
121
|
+
# @rbs node: untyped
|
|
122
|
+
#: (untyped) -> untyped
|
|
123
|
+
def type_of(node)
|
|
124
|
+
return nil if node.nil?
|
|
125
|
+
return nil if node.type == :send && NIL_HIDING_CALLS.include?(node.children[1])
|
|
126
|
+
# An instance variable read before it is set is nil whatever its annotation says.
|
|
127
|
+
return nil if node.type == :ivar
|
|
128
|
+
# `x = h[k]` is as nil as `h[k]`.
|
|
129
|
+
return type_of(node.children.last) if node.type == :lvasgn && node.children.size == 2
|
|
130
|
+
return type_of(node.children[0]) if node.type == :begin && node.children.size == 1
|
|
131
|
+
|
|
132
|
+
raw_type(node)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# @rbs node: untyped
|
|
136
|
+
#: (untyped) -> untyped
|
|
137
|
+
def raw_type(node)
|
|
138
|
+
@typing.type_of(node: node)
|
|
139
|
+
rescue StandardError
|
|
140
|
+
nil
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# @rbs name: String
|
|
144
|
+
#: (String) -> Array[String]?
|
|
145
|
+
def ancestors(name)
|
|
146
|
+
return @ancestors[name] if @ancestors.key?(name)
|
|
147
|
+
|
|
148
|
+
@ancestors[name] = begin
|
|
149
|
+
@builder.ancestor_builder.instance_ancestors(RBS::TypeName.parse(name)).ancestors.map { _1.name.to_s }
|
|
150
|
+
rescue StandardError
|
|
151
|
+
nil
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# @rbs name: String
|
|
156
|
+
#: (String) -> untyped
|
|
157
|
+
def instance_definition(name)
|
|
158
|
+
return @definitions[name] if @definitions.key?(name)
|
|
159
|
+
|
|
160
|
+
@definitions[name] = begin
|
|
161
|
+
@builder.build_instance(RBS::TypeName.parse(name))
|
|
162
|
+
rescue StandardError
|
|
163
|
+
nil
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# @rbs name: String
|
|
168
|
+
#: (String) -> untyped
|
|
169
|
+
def singleton_definition(name)
|
|
170
|
+
@builder.build_singleton(RBS::TypeName.parse(name))
|
|
171
|
+
rescue StandardError
|
|
172
|
+
nil
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# @rbs name: String
|
|
176
|
+
#: (String) -> bool
|
|
177
|
+
def class_entry?(name)
|
|
178
|
+
@builder.env.class_decls[RBS::TypeName.parse(name)].is_a?(RBS::Environment::ClassEntry)
|
|
179
|
+
rescue StandardError
|
|
180
|
+
false
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# @rbs type_name: String
|
|
184
|
+
# @rbs method: Symbol
|
|
185
|
+
#: (String, Symbol) -> String?
|
|
186
|
+
def method_owner(type_name, method)
|
|
187
|
+
instance_definition(type_name)&.methods&.[](method)&.defined_in&.to_s
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# @rbs node: untyped
|
|
191
|
+
#: (untyped) -> String?
|
|
192
|
+
def const_name(node)
|
|
193
|
+
type = raw_type(node)
|
|
194
|
+
type.is_a?(Steep::AST::Types::Name::Singleton) ? type.name.to_s : nil
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# --- nil and conditions ---
|
|
198
|
+
|
|
199
|
+
# @rbs node: untyped
|
|
200
|
+
#: (untyped) -> void
|
|
201
|
+
def safe_navigation(node)
|
|
202
|
+
receiver = node.children[0]
|
|
203
|
+
type = type_of(receiver)
|
|
204
|
+
return unless never_nil?(type)
|
|
205
|
+
|
|
206
|
+
add(node, "UnnecessarySafeNavigation", "`#{src(receiver)}` is `#{type}`, never nil, so `&.` is not needed")
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# @rbs node: untyped
|
|
210
|
+
# @rbs parent: untyped
|
|
211
|
+
#: (untyped, untyped) -> void
|
|
212
|
+
def send_rules(node, parent)
|
|
213
|
+
receiver, name, *args = node.children
|
|
214
|
+
nil_check(node, receiver, name, args)
|
|
215
|
+
type_check(node, receiver, name, args)
|
|
216
|
+
boolean_compare(node, receiver, name, args)
|
|
217
|
+
conversion(node, receiver, name, args) unless parent && BLOCKS.include?(parent.type) && parent.children[0] == node
|
|
218
|
+
literal_comparison(node, receiver, name, args)
|
|
219
|
+
unused_result(node, parent, receiver, name)
|
|
220
|
+
void_used(node, parent)
|
|
221
|
+
untyped_call(node, receiver)
|
|
222
|
+
sort_comparable(node, parent, receiver, name, args)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# @rbs node: untyped
|
|
226
|
+
# @rbs receiver: untyped
|
|
227
|
+
# @rbs name: Symbol
|
|
228
|
+
# @rbs args: Array[untyped]
|
|
229
|
+
#: (untyped, untyped, Symbol, Array[untyped]) -> void
|
|
230
|
+
def nil_check(node, receiver, name, args)
|
|
231
|
+
return unless receiver
|
|
232
|
+
return unless (name == :nil? && args.empty?) || (%i[== !=].include?(name) && args[0]&.type == :nil)
|
|
233
|
+
|
|
234
|
+
type = type_of(receiver)
|
|
235
|
+
return unless never_nil?(type)
|
|
236
|
+
|
|
237
|
+
add(node, "UnnecessaryNilCheck", "`#{src(receiver)}` is `#{type}`, never nil, so this check is always the same")
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# @rbs cond: untyped
|
|
241
|
+
# @rbs kind: Symbol
|
|
242
|
+
#: (untyped, Symbol) -> void
|
|
243
|
+
def condition(cond, kind)
|
|
244
|
+
return if cond.nil? || %i[true false].include?(cond.type)
|
|
245
|
+
# UnnecessaryNilCheck and UnnecessarySafeNavigation report these.
|
|
246
|
+
return if (cond.type == :send && cond.children[1] == :nil?) || cond.type == :csend
|
|
247
|
+
|
|
248
|
+
type = type_of(cond)
|
|
249
|
+
return unless never_falsy?(type)
|
|
250
|
+
return if members(type).all? { _1.is_a?(Steep::AST::Types::Literal) && _1.value == true }
|
|
251
|
+
|
|
252
|
+
what = kind == :and ? "the left side of `&&`" : "this condition"
|
|
253
|
+
add(cond, "AlwaysTruthyCondition", "`#{src(cond)}` is `#{type}`, never nil or false, so #{what} is always true (0 and \"\" are true in Ruby)")
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# @rbs node: untyped
|
|
257
|
+
#: (untyped) -> void
|
|
258
|
+
def or_rules(node)
|
|
259
|
+
left, right = node.children
|
|
260
|
+
type = type_of(left)
|
|
261
|
+
return unless type
|
|
262
|
+
|
|
263
|
+
if never_falsy?(type)
|
|
264
|
+
add(node, "UnnecessaryDefault", "`#{src(left)}` is `#{type}`, never nil or false, so the right side of `||` is never used")
|
|
265
|
+
elsif known?(type) && members(type).any? { can_be_false?(_1) } && members(type).any? { nil_type?(_1) } && !boolean_logic?(right)
|
|
266
|
+
add(node, "OrReplacesFalse", "`#{src(left)}` is `#{type}`; `||` replaces false as well as nil. Use `x.nil? ? default : x` to replace only nil")
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# `a&.x? || b&.y?` is logic, not a default: false there is meant to fall
|
|
271
|
+
# through. `flag || true` is a default, and the very bug this rule is for.
|
|
272
|
+
#
|
|
273
|
+
# @rbs right: untyped
|
|
274
|
+
#: (untyped) -> bool
|
|
275
|
+
def boolean_logic?(right)
|
|
276
|
+
return false if %i[true false].include?(right.type)
|
|
277
|
+
|
|
278
|
+
type = type_of(right)
|
|
279
|
+
type.nil? || !known?(type) || members(type).all? { bool_type?(_1) || nil_type?(_1) }
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# @rbs node: untyped
|
|
283
|
+
# @rbs receiver: untyped
|
|
284
|
+
# @rbs name: Symbol
|
|
285
|
+
# @rbs args: Array[untyped]
|
|
286
|
+
#: (untyped, untyped, Symbol, Array[untyped]) -> void
|
|
287
|
+
def type_check(node, receiver, name, args)
|
|
288
|
+
if %i[is_a? kind_of?].include?(name) && receiver && args.size == 1
|
|
289
|
+
class_check(node, receiver, args[0])
|
|
290
|
+
elsif name == :=== && receiver&.type == :const && args.size == 1
|
|
291
|
+
class_check(node, args[0], receiver)
|
|
292
|
+
elsif name == :respond_to? && receiver && args.size == 1 && args[0].type == :sym
|
|
293
|
+
respond_check(node, receiver, args[0].children[0])
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# @rbs node: untyped
|
|
298
|
+
# @rbs value: untyped
|
|
299
|
+
# @rbs const: untyped
|
|
300
|
+
#: (untyped, untyped, untyped) -> void
|
|
301
|
+
def class_check(node, value, const)
|
|
302
|
+
klass = const_name(const) or return
|
|
303
|
+
type = type_of(value)
|
|
304
|
+
return unless type && known?(type) && !type.is_a?(Steep::AST::Types::Intersection)
|
|
305
|
+
|
|
306
|
+
names = members(type).map { nil_type?(_1) ? "::NilClass" : instance_name(_1) }
|
|
307
|
+
return if names.any?(&:nil?)
|
|
308
|
+
|
|
309
|
+
lists = names.map { ancestors(_1.to_s) }
|
|
310
|
+
return if lists.any?(&:nil?)
|
|
311
|
+
|
|
312
|
+
if lists.all? { _1&.include?(klass) }
|
|
313
|
+
add(node, "UnnecessaryTypeCheck", "`#{src(value)}` is `#{type}`, always a `#{klass}`, so this check is always true")
|
|
314
|
+
elsif disjoint?(klass, names, lists)
|
|
315
|
+
add(node, "UnnecessaryTypeCheck", "`#{src(value)}` is `#{type}`, never a `#{klass}`, so this check is always false")
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Only classes can be disjoint: any object can include a module.
|
|
320
|
+
#
|
|
321
|
+
# @rbs klass: String
|
|
322
|
+
# @rbs names: Array[String?]
|
|
323
|
+
# @rbs lists: Array[Array[String]?]
|
|
324
|
+
#: (String, Array[String?], Array[Array[String]?]) -> bool
|
|
325
|
+
def disjoint?(klass, names, lists)
|
|
326
|
+
return false unless class_entry?(klass) && names.all? { class_entry?(_1.to_s) }
|
|
327
|
+
|
|
328
|
+
names.zip(lists).none? { |name, list| list&.include?(klass) || ancestors(klass)&.include?(name) }
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# @rbs node: untyped
|
|
332
|
+
# @rbs receiver: untyped
|
|
333
|
+
# @rbs method: Symbol
|
|
334
|
+
#: (untyped, untyped, Symbol) -> void
|
|
335
|
+
def respond_check(node, receiver, method)
|
|
336
|
+
type = type_of(receiver)
|
|
337
|
+
name = instance_name(type) or return
|
|
338
|
+
meth = instance_definition(name)&.methods&.[](method) or return
|
|
339
|
+
return unless meth.accessibility == :public
|
|
340
|
+
|
|
341
|
+
add(node, "UnnecessaryTypeCheck", "`#{src(receiver)}` is `#{type}`, which has a public `#{method}`, so `respond_to?` is always true")
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# @rbs node: untyped
|
|
345
|
+
# @rbs receiver: untyped
|
|
346
|
+
# @rbs name: Symbol
|
|
347
|
+
# @rbs args: Array[untyped]
|
|
348
|
+
#: (untyped, untyped, Symbol, Array[untyped]) -> void
|
|
349
|
+
def boolean_compare(node, receiver, name, args)
|
|
350
|
+
return unless receiver && %i[== !=].include?(name) && %i[true false].include?(args[0]&.type)
|
|
351
|
+
|
|
352
|
+
type = type_of(receiver)
|
|
353
|
+
return unless type && known?(type) && members(type).all? { bool_type?(_1) }
|
|
354
|
+
|
|
355
|
+
add(node, "UnnecessaryBooleanCompare", "`#{src(receiver)}` is `#{type}`, so compare it without `#{name} #{args[0].type}`")
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
# @rbs node: untyped
|
|
359
|
+
# @rbs receiver: untyped
|
|
360
|
+
# @rbs name: Symbol
|
|
361
|
+
# @rbs args: Array[untyped]
|
|
362
|
+
#: (untyped, untyped, Symbol, Array[untyped]) -> void
|
|
363
|
+
def conversion(node, receiver, name, args)
|
|
364
|
+
if receiver && args.empty? && (want = CONVERSIONS[name])
|
|
365
|
+
type = type_of(receiver)
|
|
366
|
+
return unless instance_name(type) == want
|
|
367
|
+
|
|
368
|
+
add(node, "UnnecessaryConversion", "`#{src(receiver)}` is already `#{type}`, so `.#{name}` returns the same value")
|
|
369
|
+
elsif receiver.nil? && args.size == 1 && (want = KERNEL_CONVERSIONS[name])
|
|
370
|
+
type = type_of(args[0])
|
|
371
|
+
return unless instance_name(type) == want
|
|
372
|
+
|
|
373
|
+
add(node, "UnnecessaryConversion", "`#{src(args[0])}` is already `#{type}`, so `#{name}(...)` returns the same value")
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# [readable, value] for a literal node; nil for anything else.
|
|
378
|
+
#
|
|
379
|
+
# @rbs node: untyped
|
|
380
|
+
#: (untyped) -> [bool, untyped]
|
|
381
|
+
def literal_value(node)
|
|
382
|
+
case node.type
|
|
383
|
+
when :sym, :str, :int then [true, node.children[0]]
|
|
384
|
+
when :nil then [true, nil]
|
|
385
|
+
else [false, nil]
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
# @rbs node: untyped
|
|
390
|
+
# @rbs receiver: untyped
|
|
391
|
+
# @rbs name: Symbol
|
|
392
|
+
# @rbs args: Array[untyped]
|
|
393
|
+
#: (untyped, untyped, Symbol, Array[untyped]) -> void
|
|
394
|
+
def literal_comparison(node, receiver, name, args)
|
|
395
|
+
return unless receiver && %i[== !=].include?(name) && args.size == 1
|
|
396
|
+
|
|
397
|
+
readable, value = literal_value(args[0])
|
|
398
|
+
return unless readable && !value.nil?
|
|
399
|
+
|
|
400
|
+
type = type_of(receiver)
|
|
401
|
+
return unless type && literal_union?(type) && !includes_literal?(type, value)
|
|
402
|
+
|
|
403
|
+
add(node, "ImpossibleLiteralComparison",
|
|
404
|
+
"`#{src(receiver)}` is `#{type}`, never `#{value.inspect}`, so `#{name}` is always #{name == :== ? "false" : "true"}")
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
# --- results ---
|
|
408
|
+
|
|
409
|
+
# @rbs node: untyped
|
|
410
|
+
# @rbs parent: untyped
|
|
411
|
+
#: (untyped, untyped) -> bool
|
|
412
|
+
def statement?(node, parent)
|
|
413
|
+
!parent.nil? && %i[begin kwbegin].include?(parent.type) && !parent.children.last.equal?(node)
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
# @rbs node: untyped
|
|
417
|
+
# @rbs parent: untyped
|
|
418
|
+
# @rbs receiver: untyped
|
|
419
|
+
# @rbs name: Symbol
|
|
420
|
+
#: (untyped, untyped, untyped, Symbol) -> void
|
|
421
|
+
def unused_result(node, parent, receiver, name)
|
|
422
|
+
return unless statement?(node, parent) && receiver
|
|
423
|
+
return if name.end_with?("!", "?", "=") || name == :freeze
|
|
424
|
+
|
|
425
|
+
klass = instance_name(type_of(receiver))
|
|
426
|
+
return unless klass && VALUE_CLASSES.include?(klass) && bang?(klass, name)
|
|
427
|
+
|
|
428
|
+
add(node, "UnusedResult", "`#{src(node)}` returns a new #{klass.delete_prefix("::")} and its result is not used; `#{name}!` changes the receiver")
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
# @rbs klass: String
|
|
432
|
+
# @rbs name: Symbol
|
|
433
|
+
#: (String, Symbol) -> bool
|
|
434
|
+
def bang?(klass, name) = instance_definition(klass)&.methods&.key?(:"#{name}!") || false
|
|
435
|
+
|
|
436
|
+
# @rbs node: untyped
|
|
437
|
+
# @rbs parent: untyped
|
|
438
|
+
#: (untyped, untyped) -> void
|
|
439
|
+
def void_used(node, parent)
|
|
440
|
+
return unless parent && raw_type(node).is_a?(Steep::AST::Types::Void)
|
|
441
|
+
|
|
442
|
+
# Not `return`: `return log(...)` is an early exit, not a use.
|
|
443
|
+
used = case parent.type
|
|
444
|
+
when :lvasgn, :ivasgn, :gvasgn, :cvasgn, :casgn, :op_asgn, :array, :pair, :dstr then true
|
|
445
|
+
when :send, :csend then !parent.children[0].equal?(node)
|
|
446
|
+
when :if then parent.children[0].equal?(node)
|
|
447
|
+
else false
|
|
448
|
+
end
|
|
449
|
+
add(node, "VoidValueUsed", "`#{src(node)}` returns void, so its value means nothing here") if used
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
# @rbs node: untyped
|
|
453
|
+
# @rbs receiver: untyped
|
|
454
|
+
#: (untyped, untyped) -> void
|
|
455
|
+
def untyped_call(node, receiver)
|
|
456
|
+
return unless receiver && raw_type(receiver).is_a?(Steep::AST::Types::Any)
|
|
457
|
+
|
|
458
|
+
add(node, "UntypedCall", "`#{src(receiver)}` is untyped, so Steep does not check `.#{node.children[1]}`")
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
# @rbs node: untyped
|
|
462
|
+
# @rbs parent: untyped
|
|
463
|
+
# @rbs receiver: untyped
|
|
464
|
+
# @rbs name: Symbol
|
|
465
|
+
# @rbs args: Array[untyped]
|
|
466
|
+
#: (untyped, untyped, untyped, Symbol, Array[untyped]) -> void
|
|
467
|
+
def sort_comparable(node, parent, receiver, name, args)
|
|
468
|
+
return unless receiver && %i[sort sort! max min minmax].include?(name) && args.empty?
|
|
469
|
+
return if parent && BLOCKS.include?(parent.type) && parent.children[0].equal?(node)
|
|
470
|
+
|
|
471
|
+
type = type_of(receiver)
|
|
472
|
+
return unless type.is_a?(Steep::AST::Types::Name::Instance) && %w[::Array ::Set].include?(type.name.to_s)
|
|
473
|
+
|
|
474
|
+
element = type.args[0]
|
|
475
|
+
return unless element && known?(element)
|
|
476
|
+
|
|
477
|
+
kinds = members(element).map { class_name(_1) }
|
|
478
|
+
return if kinds.any?(&:nil?)
|
|
479
|
+
|
|
480
|
+
kinds.uniq!
|
|
481
|
+
if kinds.size > 1 && !(kinds - %w[::Integer ::Float ::Rational]).empty?
|
|
482
|
+
add(node, "SortWithoutComparable",
|
|
483
|
+
"the elements are `#{element}`; `#{name}` raises ArgumentError when it compares two of these classes. Pass a block or `#{name}_by`")
|
|
484
|
+
elsif kinds.size == 1 && BASE_TO_S.include?(method_owner(kinds[0].to_s, :<=>))
|
|
485
|
+
add(node, "SortWithoutComparable", "`#{kinds[0]}` has only Object#<=>, so `#{name}` raises ArgumentError. Pass a block or `#{name}_by`")
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# @rbs node: untyped
|
|
490
|
+
#: (untyped) -> void
|
|
491
|
+
def interpolation(node)
|
|
492
|
+
node.children.select { _1.type == :begin }.each do |part|
|
|
493
|
+
expr = part.children[0] or next
|
|
494
|
+
# `"#{x if c}"` means "" on purpose; `$1` is checked by the match before it.
|
|
495
|
+
next if %i[if nth_ref].include?(expr.type)
|
|
496
|
+
|
|
497
|
+
type = type_of(expr)
|
|
498
|
+
next unless type && known?(type)
|
|
499
|
+
|
|
500
|
+
interpolated(node, part, expr, type)
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# @rbs node: untyped
|
|
505
|
+
# @rbs part: untyped
|
|
506
|
+
# @rbs expr: untyped
|
|
507
|
+
# @rbs type: untyped
|
|
508
|
+
#: (untyped, untyped, untyped, untyped) -> void
|
|
509
|
+
def interpolated(node, part, expr, type)
|
|
510
|
+
name = instance_name(type)
|
|
511
|
+
if node.type == :dstr && node.children.size == 1 && name == "::String"
|
|
512
|
+
add(node, "UnnecessaryConversion", "`#{src(expr)}` is already `#{type}`, so `\"\#{...}\"` returns the same value")
|
|
513
|
+
elsif nilable?(type)
|
|
514
|
+
add(part, "RestrictInterpolation", "`#{src(expr)}` is `#{type}`; nil becomes an empty string here")
|
|
515
|
+
elsif name && COLLECTIONS.include?(name)
|
|
516
|
+
add(part, "RestrictInterpolation", "`#{src(expr)}` is `#{type}`; it prints in inspect form (`[...]`, `{...}`)")
|
|
517
|
+
elsif name && BASE_TO_S.include?(method_owner(name, :to_s)) && class_entry?(name)
|
|
518
|
+
add(part, "RestrictInterpolation", "`#{src(expr)}` is `#{type}`, which has no `to_s` of its own, so it prints as `#<#{name.delete_prefix("::")}>`")
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
# --- case ---
|
|
523
|
+
|
|
524
|
+
# @rbs node: untyped
|
|
525
|
+
#: (untyped) -> void
|
|
526
|
+
def case_rules(node)
|
|
527
|
+
subject, *whens, else_body = node.children
|
|
528
|
+
return unless subject
|
|
529
|
+
|
|
530
|
+
type = type_of(subject)
|
|
531
|
+
return unless type && known?(type) && type.is_a?(Steep::AST::Types::Union)
|
|
532
|
+
|
|
533
|
+
conds = whens.flat_map { _1.children[0..-2] }
|
|
534
|
+
return if conds.any? { _1.type == :splat }
|
|
535
|
+
|
|
536
|
+
impossible_whens(subject, type, conds) if literal_union?(type)
|
|
537
|
+
exhaustive(node, subject, type, conds) unless else_body
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
# @rbs subject: untyped
|
|
541
|
+
# @rbs type: untyped
|
|
542
|
+
# @rbs conds: Array[untyped]
|
|
543
|
+
#: (untyped, untyped, Array[untyped]) -> void
|
|
544
|
+
def impossible_whens(subject, type, conds)
|
|
545
|
+
conds.each do |cond|
|
|
546
|
+
readable, value = literal_value(cond)
|
|
547
|
+
next unless readable && !value.nil? && !includes_literal?(type, value)
|
|
548
|
+
|
|
549
|
+
add(cond, "ImpossibleLiteralComparison", "`#{src(subject)}` is `#{type}`, never `#{value.inspect}`, so this `when` never matches")
|
|
550
|
+
end
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
# @rbs node: untyped
|
|
554
|
+
# @rbs subject: untyped
|
|
555
|
+
# @rbs type: untyped
|
|
556
|
+
# @rbs conds: Array[untyped]
|
|
557
|
+
#: (untyped, untyped, untyped, Array[untyped]) -> void
|
|
558
|
+
def exhaustive(node, subject, type, conds)
|
|
559
|
+
by_class = conds.any? { _1.type == :const }
|
|
560
|
+
# nil falling through to the implicit nil of `case` is the Ruby idiom, not
|
|
561
|
+
# a gap. A String member against `when "a"` is open-ended: only a
|
|
562
|
+
# class-based case can miss a class.
|
|
563
|
+
missing = members(type).reject do |member|
|
|
564
|
+
nil_type?(member) || (member.is_a?(Steep::AST::Types::Name::Instance) && !by_class) || covered?(member, conds)
|
|
565
|
+
end
|
|
566
|
+
return if missing.empty? || missing.size == members(type).size
|
|
567
|
+
|
|
568
|
+
add(node.loc.keyword, "CaseExhaustiveness",
|
|
569
|
+
"`#{src(subject)}` is `#{type}`, but no `when` handles #{missing.map { "`#{_1}`" }.join(", ")} and there is no `else`")
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
# @rbs member: untyped
|
|
573
|
+
# @rbs conds: Array[untyped]
|
|
574
|
+
#: (untyped, Array[untyped]) -> bool
|
|
575
|
+
def covered?(member, conds)
|
|
576
|
+
conds.any? do |cond|
|
|
577
|
+
readable, value = literal_value(cond)
|
|
578
|
+
if readable
|
|
579
|
+
(value.nil? && nil_type?(member)) || (member.is_a?(Steep::AST::Types::Literal) && member.value == value)
|
|
580
|
+
elsif cond.type == :const && (klass = const_name(cond))
|
|
581
|
+
name = class_name(member)
|
|
582
|
+
# A `when` on a subclass handles part of the member: stay silent rather than guess.
|
|
583
|
+
!name.nil? && (ancestors(name)&.include?(klass) || ancestors(klass)&.include?(name) || false)
|
|
584
|
+
else
|
|
585
|
+
true # a pattern this rule cannot read: assume it covers
|
|
586
|
+
end
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
# --- blocks, assertions, splats ---
|
|
591
|
+
|
|
592
|
+
# @rbs node: untyped
|
|
593
|
+
# @rbs parent: untyped
|
|
594
|
+
#: (untyped, untyped) -> void
|
|
595
|
+
def block_rules(node, parent)
|
|
596
|
+
send = node.children[0]
|
|
597
|
+
return unless send.type == :send
|
|
598
|
+
|
|
599
|
+
receiver, name = send.children
|
|
600
|
+
klass = receiver ? instance_name(type_of(receiver)) : nil
|
|
601
|
+
return unless klass
|
|
602
|
+
|
|
603
|
+
prefer_find(node, parent, name, klass)
|
|
604
|
+
return unless statement?(node, parent) && VALUE_CLASSES.include?(klass)
|
|
605
|
+
return if name.end_with?("!", "?") || ITERATORS.include?(name) || !bang?(klass, name)
|
|
606
|
+
|
|
607
|
+
add(node, "UnusedResult", "`#{src(send)} { ... }` returns a new #{klass.delete_prefix("::")} and its result is not used; `#{name}!` changes the receiver")
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# @rbs node: untyped
|
|
611
|
+
# @rbs parent: untyped
|
|
612
|
+
# @rbs name: Symbol
|
|
613
|
+
# @rbs klass: String
|
|
614
|
+
#: (untyped, untyped, Symbol, String) -> void
|
|
615
|
+
def prefer_find(node, parent, name, klass)
|
|
616
|
+
return unless parent&.type == :send && parent.children[0].equal?(node) && parent.children[1] == :first && parent.children.size == 2
|
|
617
|
+
return unless %i[select filter].include?(name) && COLLECTIONS.include?(klass)
|
|
618
|
+
|
|
619
|
+
add(parent, "PreferFind", "`#{name} { ... }.first` on #{klass.delete_prefix("::")} builds a whole list to take one; use `find { ... }`")
|
|
620
|
+
end
|
|
621
|
+
|
|
622
|
+
# @rbs node: untyped
|
|
623
|
+
#: (untyped) -> void
|
|
624
|
+
def assertion(node)
|
|
625
|
+
inner = node.children[0]
|
|
626
|
+
inner_type = raw_type(inner)
|
|
627
|
+
asserted = raw_type(node)
|
|
628
|
+
return unless inner_type && asserted
|
|
629
|
+
|
|
630
|
+
# Steep types these from the assertion itself (a literal, an empty or
|
|
631
|
+
# literal collection, nil, a generic `.new`), so the assertion is what
|
|
632
|
+
# gave them their type: `false #: bool` widens what would be `false`.
|
|
633
|
+
core = inner.type == :send && inner.children[1] == :freeze ? inner.children[0] : inner
|
|
634
|
+
return if hint_shaped?(core) || inner_type.to_s.include?("bot")
|
|
635
|
+
|
|
636
|
+
if inner_type == asserted && known?(asserted)
|
|
637
|
+
add(node, "UnnecessaryTypeAssertion", "`#{src(inner)}` is already `#{asserted}`, so the `#:` assertion can go")
|
|
638
|
+
elsif known?(inner_type) && members(inner_type).any? { nil_type?(_1) } && never_nil?(asserted)
|
|
639
|
+
add(node, "NilDroppingAssertion",
|
|
640
|
+
"`#{src(inner)}` is `#{inner_type}`; the assertion `#{asserted}` drops nil without a check. Raise or handle nil instead")
|
|
641
|
+
end
|
|
642
|
+
end
|
|
643
|
+
|
|
644
|
+
# @rbs node: untyped
|
|
645
|
+
#: (untyped) -> bool
|
|
646
|
+
def hint_shaped?(node)
|
|
647
|
+
return false unless node.is_a?(Parser::AST::Node)
|
|
648
|
+
return true if %i[array hash nil true false int float str sym lambda block numblock].include?(node.type) || (node.type == :send && node.children[1] == :new)
|
|
649
|
+
return node.children.drop(1).any? { hint_shaped?(_1) } if node.type == :if
|
|
650
|
+
return node.children.any? { hint_shaped?(_1) } if %i[begin or and].include?(node.type)
|
|
651
|
+
|
|
652
|
+
false
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
# @rbs node: untyped
|
|
656
|
+
#: (untyped) -> void
|
|
657
|
+
def splat(node)
|
|
658
|
+
value = node.children[0] or return
|
|
659
|
+
type = type_of(value)
|
|
660
|
+
return unless instance_name(type) == "::Hash"
|
|
661
|
+
|
|
662
|
+
add(node, "MisusedSplat", "`#{src(value)}` is `#{type}`; `*` splits it into [key, value] pairs. Use `**` for keywords")
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
# --- method definitions ---
|
|
666
|
+
|
|
667
|
+
# @rbs node: untyped
|
|
668
|
+
# @rbs nesting: Array[String]
|
|
669
|
+
# @rbs singleton: bool
|
|
670
|
+
#: (untyped, Array[String], bool) -> void
|
|
671
|
+
def def_rules(node, nesting, singleton)
|
|
672
|
+
return if nesting.empty?
|
|
673
|
+
|
|
674
|
+
name = node.type == :defs ? node.children[1] : node.children[0]
|
|
675
|
+
type_name = "::#{nesting.join("::")}"
|
|
676
|
+
definition = singleton ? singleton_definition(type_name) : instance_definition(type_name)
|
|
677
|
+
returns = definition&.methods&.[](name)&.method_types&.map { _1.type.return_type } || []
|
|
678
|
+
return if returns.empty?
|
|
679
|
+
|
|
680
|
+
if name.end_with?("?") && returns.none? { boolish?(_1) }
|
|
681
|
+
add(node.loc.name, "PredicateReturnType", "`#{name}` ends with `?` but returns `#{returns.join(" | ")}`; return bool or rename it")
|
|
682
|
+
end
|
|
683
|
+
body = node.children.last
|
|
684
|
+
last = body&.type == :begin ? body.children.last : body
|
|
685
|
+
return unless last&.type == :self && returns.all? { _1.is_a?(RBS::Types::ClassInstance) && _1.name.to_s == type_name }
|
|
686
|
+
|
|
687
|
+
add(node.loc.name, "PreferReturnSelfType", "`#{name}` returns `self` but its type says `#{type_name.delete_prefix("::")}`; `-> self` keeps the subclass type")
|
|
688
|
+
end
|
|
689
|
+
|
|
690
|
+
# @rbs type: untyped
|
|
691
|
+
#: (untyped) -> bool
|
|
692
|
+
def boolish?(type)
|
|
693
|
+
case type
|
|
694
|
+
when RBS::Types::Bases::Bool, RBS::Types::Bases::Any, RBS::Types::Bases::Top then true
|
|
695
|
+
when RBS::Types::Literal then [true, false].include?(type.literal)
|
|
696
|
+
when RBS::Types::Alias then type.name.to_s.delete_prefix("::") == "boolish"
|
|
697
|
+
when RBS::Types::Optional then boolish?(type.type)
|
|
698
|
+
when RBS::Types::Union then type.types.all? { boolish?(_1) || _1.is_a?(RBS::Types::Bases::Nil) }
|
|
699
|
+
else false
|
|
700
|
+
end
|
|
701
|
+
end
|
|
702
|
+
|
|
703
|
+
# Steep finds these itself, but its default diagnostic levels hide them as hints.
|
|
704
|
+
#: () -> void
|
|
705
|
+
def steep_hidden
|
|
706
|
+
@typing.errors.each do |error|
|
|
707
|
+
case error
|
|
708
|
+
when Steep::Diagnostic::Ruby::UnreachableBranch, Steep::Diagnostic::Ruby::UnreachableValueBranch
|
|
709
|
+
next if @deciders[error.node]&.any? { reads_hidden_nil?(_1) }
|
|
710
|
+
|
|
711
|
+
add(error.location, "UnreachableBranch", error.header_line)
|
|
712
|
+
when Steep::Diagnostic::Ruby::DeprecatedReference
|
|
713
|
+
add(error.location, "DeprecatedReference", error.header_line)
|
|
714
|
+
end
|
|
715
|
+
end
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
# Steep decides a branch is unreachable from the same types the rules
|
|
719
|
+
# above distrust: `h[k].nil?` is never true to it. Such a branch is kept.
|
|
720
|
+
# @rbs node: untyped
|
|
721
|
+
#: (untyped) -> bool
|
|
722
|
+
def reads_hidden_nil?(node)
|
|
723
|
+
return false unless node.is_a?(Parser::AST::Node)
|
|
724
|
+
return true if node.type == :ivar
|
|
725
|
+
return true if %i[send csend].include?(node.type) && NIL_HIDING_CALLS.include?(node.children[1])
|
|
726
|
+
|
|
727
|
+
node.children.any? { reads_hidden_nil?(_1) }
|
|
728
|
+
end
|
|
729
|
+
end
|
|
730
|
+
end
|