summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-warnings/unused_types.ml
blob: 318103e5570a77bba044a22b67c8926dc8b4eff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
(* TEST
   flags = " -w +A -strict-sequence "
   * expect
*)

module Unused : sig
end = struct
  type unused = int
end
;;
[%%expect {|
Line 3, characters 2-19:
3 |   type unused = int
      ^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type unused.
module Unused : sig end
|}]

module Unused_nonrec : sig
end = struct
  type nonrec used = int
  type nonrec unused = used
end
;;
[%%expect {|
Line 4, characters 2-27:
4 |   type nonrec unused = used
      ^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type unused.
module Unused_nonrec : sig end
|}]

module Unused_rec : sig
end = struct
  type unused = A of unused
end
;;
[%%expect {|
Line 3, characters 2-27:
3 |   type unused = A of unused
      ^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type unused.
Line 3, characters 16-27:
3 |   type unused = A of unused
                    ^^^^^^^^^^^
Warning 37 [unused-constructor]: unused constructor A.
module Unused_rec : sig end
|}]

module Used_constructor : sig
  type t
  val t : t
end = struct
  type t = T
  let t = T
end
;;
[%%expect {|
module Used_constructor : sig type t val t : t end
|}]

module Unused_constructor : sig
  type t
end = struct
  type t = T
end
;;
[%%expect {|
Line 4, characters 11-12:
4 |   type t = T
               ^
Warning 37 [unused-constructor]: unused constructor T.
module Unused_constructor : sig type t end
|}]

module Unused_constructor_outside_patterns : sig
  type t
  val nothing : t -> unit
end = struct
  type t = T
  let nothing = function
    | T -> ()
end
;;
[%%expect {|
Line 5, characters 11-12:
5 |   type t = T
               ^
Warning 37 [unused-constructor]: constructor T is never used to build values.
(However, this constructor appears in patterns.)
module Unused_constructor_outside_patterns :
  sig type t val nothing : t -> unit end
|}]

module Unused_constructor_exported_private : sig
  type t = private T
end = struct
  type t = T
end
;;
[%%expect {|
Line 4, characters 11-12:
4 |   type t = T
               ^
Warning 37 [unused-constructor]: constructor T is never used to build values.
Its type is exported as a private type.
module Unused_constructor_exported_private : sig type t = private T end
|}]

module Used_private_constructor : sig
  type t
  val nothing : t -> unit
end = struct
  type t = private T
  let nothing = function
    | T -> ()
end
;;
[%%expect {|
module Used_private_constructor : sig type t val nothing : t -> unit end
|}]

module Unused_private_constructor : sig
  type t
end = struct
  type t = private T
end
;;
[%%expect {|
Line 4, characters 19-20:
4 |   type t = private T
                       ^
Warning 37 [unused-constructor]: unused constructor T.
module Unused_private_constructor : sig type t end
|}]

module Exported_private_constructor : sig
  type t = private T
end = struct
  type t = private T
end
;;
[%%expect {|
module Exported_private_constructor : sig type t = private T end
|}]

module Used_exception : sig
  val e : exn
end = struct
  exception Somebody_uses_me
  let e = Somebody_uses_me
end
;;
[%%expect {|
module Used_exception : sig val e : exn end
|}]

module Used_extension_constructor : sig
  type t
  val t : t
end = struct
  type t = ..
  type t += Somebody_uses_me
  let t = Somebody_uses_me
end
;;
[%%expect {|
module Used_extension_constructor : sig type t val t : t end
|}]

module Unused_exception : sig
end = struct
  exception Nobody_uses_me
end
;;
[%%expect {|
Line 3, characters 2-26:
3 |   exception Nobody_uses_me
      ^^^^^^^^^^^^^^^^^^^^^^^^
Warning 38 [unused-extension]: unused exception Nobody_uses_me
module Unused_exception : sig end
|}]

module Unused_extension_constructor : sig
  type t = ..
end = struct
  type t = ..
  type t += Nobody_uses_me
end
;;
[%%expect {|
Line 5, characters 12-26:
5 |   type t += Nobody_uses_me
                ^^^^^^^^^^^^^^
Warning 38 [unused-extension]: unused extension constructor Nobody_uses_me
module Unused_extension_constructor : sig type t = .. end
|}]

module Unused_extension_disabled_warning : sig
  type t = ..
end = struct
  type t = ..
  type t += Dont_warn_on_me [@warning "-unused-extension"] | Nobody_uses_me
end
;;
[%%expect {|
Line 5, characters 59-75:
5 |   type t += Dont_warn_on_me [@warning "-unused-extension"] | Nobody_uses_me
                                                               ^^^^^^^^^^^^^^^^
Warning 38 [unused-extension]: unused extension constructor Nobody_uses_me
module Unused_extension_disabled_warning : sig type t = .. end
|}]

module Unused_exception_outside_patterns : sig
  val falsity : exn -> bool
end = struct
  exception Nobody_constructs_me
  let falsity = function
    | Nobody_constructs_me -> true
    | _ -> false
end
;;
[%%expect {|
Line 4, characters 2-32:
4 |   exception Nobody_constructs_me
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 38 [unused-extension]: exception Nobody_constructs_me is never used to build values.
(However, this constructor appears in patterns.)
module Unused_exception_outside_patterns : sig val falsity : exn -> bool end
|}]

module Unused_extension_outside_patterns : sig
  type t = ..
  val falsity : t -> bool
end = struct
  type t = ..
  type t += Noone_builds_me
  let falsity = function
    | Noone_builds_me -> true
    | _ -> false
end
;;
[%%expect {|
Line 6, characters 12-27:
6 |   type t += Noone_builds_me
                ^^^^^^^^^^^^^^^
Warning 38 [unused-extension]: extension constructor Noone_builds_me is never used to build values.
(However, this constructor appears in patterns.)
module Unused_extension_outside_patterns :
  sig type t = .. val falsity : t -> bool end
|}]

module Unused_exception_exported_private : sig
  type exn += private Private_exn
end = struct
  exception Private_exn
end
;;
[%%expect {|
Line 4, characters 2-23:
4 |   exception Private_exn
      ^^^^^^^^^^^^^^^^^^^^^
Warning 38 [unused-extension]: exception Private_exn is never used to build values.
It is exported or rebound as a private extension.
module Unused_exception_exported_private :
  sig type exn += private Private_exn end
|}]

module Unused_extension_exported_private : sig
  type t = ..
  type t += private Private_ext
end = struct
  type t = ..
  type t += Private_ext
end
;;
[%%expect {|
Line 6, characters 12-23:
6 |   type t += Private_ext
                ^^^^^^^^^^^
Warning 38 [unused-extension]: extension constructor Private_ext is never used to build values.
It is exported or rebound as a private extension.
module Unused_extension_exported_private :
  sig type t = .. type t += private Private_ext end
|}]

module Used_private_extension : sig
  type t
  val nothing : t -> unit
end = struct
  type t = ..
  type t += private Private_ext
  let nothing = function
    | Private_ext | _ -> ()
end
;;
[%%expect {|
module Used_private_extension : sig type t val nothing : t -> unit end
|}]

module Unused_private_extension : sig
  type t
end = struct
  type t = ..
  type t += private Private_ext
end
;;
[%%expect {|
Line 5, characters 20-31:
5 |   type t += private Private_ext
                        ^^^^^^^^^^^
Warning 38 [unused-extension]: unused extension constructor Private_ext
module Unused_private_extension : sig type t end
|}]

module Exported_private_extension : sig
  type t = ..
  type t += private Private_ext
end = struct
  type t = ..
  type t += private Private_ext
end
;;
[%%expect {|
module Exported_private_extension :
  sig type t = .. type t += private Private_ext end
|}]


module Pr7438 : sig
end = struct
  module type S = sig type t = private [> `Foo] end
  module type X =
    sig type t = private [> `Foo | `Bar] include S with type t := t end
end;;
[%%expect {|
module Pr7438 : sig end
|}]

module Unused_type_disable_warning : sig
end = struct
  type t = A [@@warning "-34"]
end;;
[%%expect {|
Line 3, characters 11-12:
3 |   type t = A [@@warning "-34"]
               ^
Warning 37 [unused-constructor]: unused constructor A.
module Unused_type_disable_warning : sig end
|}]

module Unused_constructor_disable_warning : sig
end = struct
  type t = A [@@warning "-37"]
end;;
[%%expect {|
Line 3, characters 2-30:
3 |   type t = A [@@warning "-37"]
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type t.
module Unused_constructor_disable_warning : sig end
|}]

module Unused_constructor_disable_one_warning : sig
end = struct
  type t = A [@warning "-37"] | B
end;;
[%%expect {|
Line 3, characters 2-33:
3 |   type t = A [@warning "-37"] | B
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type t.
Line 3, characters 30-33:
3 |   type t = A [@warning "-37"] | B
                                  ^^^
Warning 37 [unused-constructor]: unused constructor B.
module Unused_constructor_disable_one_warning : sig end
|}]

module Unused_record : sig end = struct
  type t = { a : int; b : int }
  let foo (x : t) = x
  let _ = foo
end;;
[%%expect {|
Line 2, characters 13-21:
2 |   type t = { a : int; b : int }
                 ^^^^^^^^
Warning 69 [unused-field]: unused record field a.
Line 2, characters 22-29:
2 |   type t = { a : int; b : int }
                          ^^^^^^^
Warning 69 [unused-field]: unused record field b.
module Unused_record : sig end
|}]

module Unused_field : sig end = struct
  type t = { a : int }
  let foo () = { a = 0 }
  let _ = foo
end;;
[%%expect {|
Line 2, characters 13-20:
2 |   type t = { a : int }
                 ^^^^^^^
Warning 69 [unused-field]: record field a is never read.
(However, this field is used to build or mutate values.)
module Unused_field : sig end
|}]

module Unused_field : sig end = struct
  type t = { a : int; b : int; c : int }
  let foo () = { a = 0; b = 0; c = 0 }
  let bar x = x.a
  let baz { c; _ } = c
  let _ = foo, bar, baz
end;;
[%%expect {|
Line 2, characters 22-30:
2 |   type t = { a : int; b : int; c : int }
                          ^^^^^^^^
Warning 69 [unused-field]: record field b is never read.
(However, this field is used to build or mutate values.)
module Unused_field : sig end
|}]

module Unused_mutable_field : sig end = struct
  type t = { a : int; mutable b : int }
  let foo () = { a = 0; b = 0 }
  let bar x = x.a, x.b
  let _ = foo, bar
end;;
[%%expect {|
Line 2, characters 22-37:
2 |   type t = { a : int; mutable b : int }
                          ^^^^^^^^^^^^^^^
Warning 69 [unused-field]: mutable record field b is never mutated.
module Unused_mutable_field : sig end
|}]

module Unused_field_exported_private : sig
  type t = private { a : int }
end = struct
  type t = { a : int }
end;;
[%%expect {|
module Unused_field_exported_private : sig type t = private { a : int; } end
|}]

module Unused_field_exported_private : sig
  type t = private { a : int }
end = struct
  type t = { a : int }
  let foo x = x.a
  let _ = foo
end;;
[%%expect {|
module Unused_field_exported_private : sig type t = private { a : int; } end
|}]

module Unused_mutable_field_exported_private : sig
  type t = private { a : int; mutable b : int }
end = struct
  type t = { a : int; mutable b : int }
  let foo () = { a = 0; b = 0 }
  let _ = foo
end;;
[%%expect {|
Line 4, characters 22-37:
4 |   type t = { a : int; mutable b : int }
                          ^^^^^^^^^^^^^^^
Warning 69 [unused-field]: mutable record field b is never mutated.
module Unused_mutable_field_exported_private :
  sig type t = private { a : int; mutable b : int; } end
|}]

module Unused_field_disable_warning : sig
end = struct
  type t = { a: int; b:int } [@@warning "-unused-field"]
end;;
[%%expect {|
Line 3, characters 2-56:
3 |   type t = { a: int; b:int } [@@warning "-unused-field"]
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type t.
module Unused_field_disable_warning : sig end
|}]

module Unused_field_disable_one_warning : sig
end = struct
  type t = { a: int [@warning "-unused-field"]; b:int }
end;;
[%%expect {|
Line 3, characters 2-55:
3 |   type t = { a: int [@warning "-unused-field"]; b:int }
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 34 [unused-type-declaration]: unused type t.
Line 3, characters 48-53:
3 |   type t = { a: int [@warning "-unused-field"]; b:int }
                                                    ^^^^^
Warning 69 [unused-field]: unused record field b.
module Unused_field_disable_one_warning : sig end
|}]