-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.ml
More file actions
696 lines (621 loc) · 23.9 KB
/
test.ml
File metadata and controls
696 lines (621 loc) · 23.9 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
let display_verbose = Report.Display.student_display
(* Unfortunately, I have to copy most of the library code from camelot.ml *)
let fail msg = prerr_endline msg; exit 1
let safe_open src =
try src, open_in src
with Sys_error msg -> fail msg
let to_ast file =
let src, f = safe_open file in
src, ( f |> Lexing.from_channel |> Parse.implementation )
let line_lint : bool ref = ref false
let lint_and_hint : (string * Parsetree.structure) -> unit = fun (file, ast) ->
let store : Canonical.Hint.hint list ref = ref [] in
let line_length_lint : string -> unit = fun file ->
if not !line_lint then ()
else
let chan = open_in file in
let lref : int ref = ref 1 in
try
while true; do
let line = input_line chan in
(if (String.length line > 80) then store := Canonical.Hint.line_hint file !lref line :: !store;);
incr lref
done; ()
with End_of_file ->
close_in chan; () in
line_length_lint file;
file |>
Traverse.Iter.make_linterator store |>
Traverse.Iter.apply_iterator ast;
display_verbose !store
(* Run the tests in lexical.ml *)
let%expect_test _ =
line_lint := true;
let file : string = "./examples/lexical.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
line_lint := false;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/lexical.ml, line 5, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
let verylongvariablenamethisispainful = [1;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1]
Consider:
indenting to avoid exceeding the 80 character line limit
(* ------------------------------------------------------------------------ *)
File ./examples/lexical.ml, line 2, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
let verylongvariablenamethisispainful = [1;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1]
Consider:
indenting to avoid exceeding the 80 character line limit
(* ------------------------------------------------------------------------ *)
File ./examples/lexical.ml, line 5, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
let verylongvariablenamethisispainful = [1;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1]
Consider:
indenting to avoid exceeding the 80 character line limit
(* ------------------------------------------------------------------------ *)
File ./examples/lexical.ml, line 2, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
let verylongvariablenamethisispainful = [1;2;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1]
Consider:
indenting to avoid exceeding the 80 character line limit |}]
(* Run the tests in equality.ml *)
let%expect_test _ =
let file : string = "./examples/equality.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 29, columns: 8-26
Warning:
using `!=` when structural equality is intended
You wrote:
"ocaml" != "ocaml"
Consider:
using `<>` to evaluate structural inequality
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 28, columns: 8-14
Warning:
using `!=` when structural equality is intended
You wrote:
1 != 1
Consider:
using `<>` to evaluate structural inequality
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 27, columns: 8-26
Warning:
using `==` when structural equality is intended
You wrote:
"ocaml" == "ocaml"
Consider:
using `=` to evaluate structural equality
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 26, columns: 8-14
Warning:
using `==` when structural equality is intended
You wrote:
1 == 1
Consider:
using `=` to evaluate structural equality
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 23, columns: 9-23
Warning:
using `=` with a boolean literal
You wrote:
false = bfalse
Consider:
using the variable itself to represent the value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 22, columns: 9-21
Warning:
using `=` with a boolean literal
You wrote:
true = btrue
Consider:
using the variable itself to represent the value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 21, columns: 9-23
Warning:
using `=` with a boolean literal
You wrote:
bfalse = false
Consider:
using the variable itself to represent the value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 20, columns: 9-21
Warning:
using `=` with a boolean literal
You wrote:
btrue = true
Consider:
using the variable itself to represent the value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 15, columns: 8-41
Warning:
using `=` with lists as a condition in an if statement
You wrote:
if [1; 2; 3] = q then None else x
Consider:
using a pattern match to check whether a list has a certain value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 14, columns: 8-37
Warning:
using `=` with lists as a condition in an if statement
You wrote:
if q = [1] then x else None
Consider:
using a pattern match to check whether a list has a certain value
(* ------------------------------------------------------------------------ *)
File ./examples/equality.ml, line 5, columns: 8-23
Warning:
using `=` with options
You wrote:
(Some 1) = (Some 1)
Consider:
using a pattern match to check the presence of an option
|}]
(* Run the tests in verbose.ml *)
let%expect_test _ =
let file : string = "./examples/verbose.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 87, columns: 8-57
Warning:
Usage of the `&&` is redundant
You wrote:
(x = (TConstr 3)) && ((x = (TConstr 4)) && (x = (TConstr 3)))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 86, columns: 8-57
Warning:
Usage of the `&&` is redundant
You wrote:
(x = (TConstr 3)) && ((x = (TConstr 3)) && (x = (TConstr 4)))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 85, columns: 8-40
Warning:
Usage of the `&&` is redundant
You wrote:
(x = (TConstr 3)) && (x = (TConstr 3))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 84, columns: 8-24
Warning:
Usage of the `&&` is redundant
You wrote:
(x = 5) && (x = 5)
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 83, columns: 8-30
Warning:
Usage of the `&&` is redundant
You wrote:
(x = None) && (x = None)
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 82, columns: 8-26
Warning:
Usage of the `&&` is redundant
You wrote:
(x = []) && (x = [])
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 81, columns: 8-57
Warning:
Usage of the `||` is redundant
You wrote:
(x = (TConstr 3)) || ((x = (TConstr 4)) || (x = (TConstr 3)))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 80, columns: 8-57
Warning:
Usage of the `||` is redundant
You wrote:
(x = (TConstr 3)) || ((x = (TConstr 3)) || (x = (TConstr 4)))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 79, columns: 8-40
Warning:
Usage of the `||` is redundant
You wrote:
(x = (TConstr 3)) || (x = (TConstr 3))
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 78, columns: 8-24
Warning:
Usage of the `||` is redundant
You wrote:
(x = 5) || (x = 5)
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 77, columns: 8-30
Warning:
Usage of the `||` is redundant
You wrote:
(x = None) || (x = None)
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 76, columns: 8-26
Warning:
Usage of the `||` is redundant
You wrote:
(x = []) || (x = [])
Consider:
simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 62, columns: 8-85
Warning:
using nested if statements more than three layers deep
You wrote:
if x then 1 else if y then 2 else if x & y then 3 else if z = 4 then 3 else 9
Consider:
using let statements or helper methods / rethinking logic
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, lines 44-55, columns: 2-5
Warning:
using nested match statements three or more layers deep
You wrote:
match l with
| [] ->
(match l with
| [] -> let z = [] in (match z with | _ -> true)
| _ -> false)
| _ -> true
Consider:
using let statements or helper methods / rethinking logic
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 11, columns: 31-36
Warning:
using fst / snd to project values out of a tuple
You wrote:
snd t
Consider:
using a let pattern match statement instead
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 11, columns: 23-28
Warning:
using fst / snd to project values out of a tuple
You wrote:
fst t
Consider:
using a let pattern match statement instead
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 6, columns: 10-21
Warning:
using `@` to prepend an element to a list
You wrote:
1 :: [] @ t
Consider:
using `::` instead
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 3, columns: 10-17
Warning:
using `@` to prepend an element to a list
You wrote:
[1] @ t
Consider:
using `::` instead
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 62, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
let z = if x then 1 else if y then 2 else if x & y then 3 else if z = 4 then 3 else 9
Consider:
indenting to avoid exceeding the 80 character line limit
(* ------------------------------------------------------------------------ *)
File ./examples/verbose.ml, line 13, columns: 0-80
Warning:
exceeding the 80 character line limit
You wrote:
(* Nested ifs :( - we skip local lets and sequencing to get the actual return type for now *)
Consider:
indenting to avoid exceeding the 80 character line limit
|}]
(* Run the tests in if.ml *)
let%expect_test _ =
let file : string = "./examples/if.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 60, columns: 14-48
Warning:
overly verbose if statement that can be simplified
You wrote:
if p h then nonsense p t else true
Consider:
rewriting using a boolean operator like `||` and `not`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 55, columns: 14-45
Warning:
overly verbose if statement that can be simplified
You wrote:
if p h then false else none p t
Consider:
rewriting using a boolean operator like `&&` and `not`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 50, columns: 14-47
Warning:
overly verbose if statement that can be simplified
You wrote:
if p h then forall p t else false
Consider:
rewriting using a boolean operator like `&&`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 45, columns: 14-48
Warning:
overly verbose if statement that can be simplified
You wrote:
if h = i then true else exists t i
Consider:
rewriting using a boolean operator like `||`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 38, columns: 9-39
Warning:
returning the condition of an if statement on success and a boolean literal otherwise
You wrote:
if 3 > 0 then 3 > 0 else false
Consider:
returning just the condition or simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 34, columns: 20-41
Warning:
overly verbose if statement that can be simplified
You wrote:
if x then x else true
Consider:
rewriting using a boolean operator like `||` and `not`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 34, columns: 20-41
Warning:
returning the condition of an if statement on success and a boolean literal otherwise
You wrote:
if x then x else true
Consider:
returning just the condition or simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 31, columns: 9-30
Warning:
overly verbose if statement that can be simplified
You wrote:
if x then y else true
Consider:
rewriting using a boolean operator like `||` and `not`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 28, columns: 9-31
Warning:
overly verbose if statement that can be simplified
You wrote:
if x then false else y
Consider:
rewriting using a boolean operator like `&&` and `not`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 25, columns: 9-31
Warning:
overly verbose if statement that can be simplified
You wrote:
if x then y else false
Consider:
rewriting using a boolean operator like `&&`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 22, columns: 9-30
Warning:
overly verbose if statement that can be simplified
You wrote:
if x then true else y
Consider:
rewriting using a boolean operator like `||`
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 19, columns: 9-31
Warning:
checking negation in the if condition
You wrote:
if not e then x else y
Consider:
swapping the then and else branches of the if statement
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 16, columns: 9-37
Warning:
returning the condition of an if statement on success and a boolean literal otherwise
You wrote:
if beta then beta else false
Consider:
returning just the condition or simplifying further
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 13, columns: 9-34
Warning:
using an if statement to return `true | false` literally
You wrote:
if e then false else true
Consider:
returning just the condition (+ some tweaks)
(* ------------------------------------------------------------------------ *)
File ./examples/if.ml, line 10, columns: 9-34
Warning:
using an if statement to return `true | false` literally
You wrote:
if e then true else false
Consider:
returning just the condition (+ some tweaks)
|}]
(* Run the tests in match.ml *)
let%expect_test _ =
let file : string = "./examples/match.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 112, columns: 13-20
Warning:
using an overly complex match clause
You wrote:
| y :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [y] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 112, columns: 4-11
Warning:
using an overly complex match clause
You wrote:
| x :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [x] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 106, columns: 7-14
Warning:
using an overly complex match clause
You wrote:
| x :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [x] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 100, columns: 4-11
Warning:
using an overly complex match clause
You wrote:
| x :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [x] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, lines 81-82, columns: 2-15
Warning:
using pattern matching on a tuple (for fewer than 2 cases)
You wrote:
match r with | (x, y) -> ()
Consider:
using a let statement to extract tuple fields
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, lines 69-71, columns: 2-16
Warning:
using pattern matching on a record (for fewer than 3 cases)
You wrote:
match r with | { x; y } -> () | { x;_} -> ()
Consider:
using a let statement to extract record fields
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, lines 65-66, columns: 2-16
Warning:
using pattern matching on a record (for fewer than 3 cases)
You wrote:
match r with | { x; y } -> ()
Consider:
using a let statement to extract record fields
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 48, columns: 4-13
Warning:
using an overly complex match clause
You wrote:
| abc :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [abc] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 47, columns: 4-11
Warning:
using an overly complex match clause
You wrote:
| x :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [x] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, line 42, columns: 4-11
Warning:
using an overly complex match clause
You wrote:
| _ :: [] -> ...
Consider:
expressing this match case more compactly, such as: | [_] -> ...
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, lines 12-15, columns: 15-5
Warning:
using integer pattern matching on fewer than 3 cases
You wrote:
match b with | 2 -> true | 3 -> false
Consider:
using an if statement and `=`
(* ------------------------------------------------------------------------ *)
File ./examples/match.ml, lines 4-7, columns: 15-5
Warning:
using pattern matching on boolean literals
You wrote:
match b with | false -> true | true -> false
Consider:
using an if statement or boolean operators
|}]
(* Run the tests in hof.ml *)
let%expect_test _ =
let file : string = "./examples/hof.ml" in
let to_lint = to_ast file in
lint_and_hint to_lint;
[%expect{|
(* ------------------------------------------------------------------------ *)
File ./examples/hof.ml, lines 35-39, columns: 0-5
Warning:
overly verbose function implementation
You wrote:
let rec fold (combine : 'a -> 'b -> 'b) (base : 'b) (l : 'a list) : 'b=
match l with | [] -> base | x::xs -> combine x (fold combine base xs)
Consider:
using a higher order function like fold
(* ------------------------------------------------------------------------ *)
File ./examples/hof.ml, lines 17-20, columns: 0-31
Warning:
overly verbose function implementation
You wrote:
let rec sum_verbose (l : int list) =
match l with | [] -> 0 | h::t -> h + (sum_verbose t)
Consider:
using a higher order function like fold
(* ------------------------------------------------------------------------ *)
File ./examples/hof.ml, lines 11-15, columns: 0-13
Warning:
overly verbose function implementation
You wrote:
let rec print_l (l : int list) =
match l with
| [] -> ()
| h::t -> ((h |> string_of_int) |> print_endline; print_l t)
Consider:
using a higher order function like iter
(* ------------------------------------------------------------------------ *)
File ./examples/hof.ml, lines 6-9, columns: 0-33
Warning:
overly verbose function implementation
You wrote:
let rec plus_n n l =
match l with | [] -> [] | h::t -> (h + n) :: (plus_n n t)
Consider:
using a higher order function like transform
(* ------------------------------------------------------------------------ *)
File ./examples/hof.ml, lines 1-4, columns: 0-34
Warning:
overly verbose function implementation
You wrote:
let rec plus_one l =
match l with | [] -> [] | h::t -> (h + 1) :: (plus_one t)
Consider:
using a higher order function like transform
|}]