Skip to content

Commit 7d326bd

Browse files
committed
[docs][field_ctor][5/n] add inline record example
1 parent afbec73 commit 7d326bd

15 files changed

Lines changed: 200 additions & 12 deletions

File tree

check/classic/classic.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ Nothing else to report in this section
240240
====================================
241241
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
242242

243+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
244+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
245+
243246
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
244247
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
245248

check/classic/classic.ref

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ Nothing else to report in this section
245245
====================================
246246
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
247247

248+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
249+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
250+
248251
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
249252
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
250253

@@ -661,7 +664,7 @@ Nothing else to report in this section
661664
--------------------------------------------------------------------------------
662665

663666

664-
Total: 560
665-
Success: 553
667+
Total: 562
668+
Success: 555
666669
Failed: 7
667-
Ratio: 98.75%
670+
Ratio: 98.7544483986%

check/internal/internal.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ Nothing else to report in this section
195195
====================================
196196
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
197197

198+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
199+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
200+
198201
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
199202
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
200203

check/internal/internal.ref

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ Nothing else to report in this section
200200
====================================
201201
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
202202

203+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
204+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
205+
203206
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
204207
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
205208

@@ -616,7 +619,7 @@ Nothing else to report in this section
616619
--------------------------------------------------------------------------------
617620

618621

619-
Total: 518
620-
Success: 511
622+
Total: 520
623+
Success: 513
621624
Failed: 7
622-
Ratio: 98.6486486486%
625+
Ratio: 98.6538461538%

check/threshold-1/threshold-1.exp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:6: gadt_of_int
144144
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:8: float_opt_of_gadt
145145

146+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:7: get_left_opt
147+
146148
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int
147149
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly
148150

@@ -550,6 +552,9 @@ Nothing else to report in this section
550552
====================================
551553
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
552554

555+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
556+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
557+
553558
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
554559
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
555560

@@ -627,6 +632,8 @@ Nothing else to report in this section
627632
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
628633
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:3: gadt.Int
629634

635+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:3: t.Both
636+
630637
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Left
631638
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.right
632639

check/threshold-1/threshold-1.ref

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:6: gadt_of_int
149149
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:8: float_opt_of_gadt
150150

151+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:7: get_left_opt
152+
151153
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int
152154
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly
153155

@@ -554,6 +556,9 @@ Nothing else to report in this section
554556
====================================
555557
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
556558

559+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
560+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
561+
557562
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
558563
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
559564

@@ -631,6 +636,8 @@ Nothing else to report in this section
631636
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
632637
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:3: gadt.Int
633638

639+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:3: t.Both
640+
634641
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Left
635642
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.right
636643

@@ -1049,7 +1056,7 @@ Nothing else to report in this section
10491056
--------------------------------------------------------------------------------
10501057

10511058

1052-
Total: 885
1053-
Success: 877
1059+
Total: 889
1060+
Success: 881
10541061
Failed: 8
1055-
Ratio: 99.0960451977%
1062+
Ratio: 99.1001124859%

check/threshold-3-0.5/threshold-3-0.5.exp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:6: gadt_of_int
144144
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:8: float_opt_of_gadt
145145

146+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:7: get_left_opt
147+
146148
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int
147149
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly
148150

@@ -727,6 +729,9 @@ Nothing else to report in this section
727729
====================================
728730
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
729731

732+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
733+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
734+
730735
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
731736
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
732737

@@ -804,6 +809,8 @@ Nothing else to report in this section
804809
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
805810
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:3: gadt.Int
806811

812+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:3: t.Both
813+
807814
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Left
808815
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.right
809816

check/threshold-3-0.5/threshold-3-0.5.ref

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:6: gadt_of_int
149149
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:8: float_opt_of_gadt
150150

151+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:7: get_left_opt
152+
151153
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int
152154
./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly
153155

@@ -731,6 +733,9 @@ Nothing else to report in this section
731733
====================================
732734
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:4: gadt.Float
733735

736+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
737+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
738+
734739
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Right
735740
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.left
736741

@@ -808,6 +813,8 @@ Nothing else to report in this section
808813
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
809814
./examples/docs/fields_and_constructors/code_constructs/gadt/gadt_lib.mli:3: gadt.Int
810815

816+
./examples/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:3: t.Both
817+
811818
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:2: either.Left
812819
./examples/docs/fields_and_constructors/code_constructs/polymorphic_type/polymorphic_type_lib.mli:3: both.right
813820

@@ -1448,7 +1455,7 @@ Nothing else to report in this section
14481455
--------------------------------------------------------------------------------
14491456

14501457

1451-
Total: 1202
1452-
Success: 1194
1458+
Total: 1206
1459+
Success: 1198
14531460
Failed: 8
1454-
Ratio: 99.3344425957%
1461+
Ratio: 99.3366500829%

docs/fields_and_constructors/FIELDS_AND_CONSTRUCTORS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ The expected resolution for an unused exported value is to remove it from the
191191
- [Polymorphic type](./code_constructs/POLYMORPHIC_TYPE.md)
192192
- [Polymorphic variant](./code_constructs/POLYMORPHIC_VARIANT.md)
193193
- [GADT](./code_constructs/GADT.md)
194+
- [Inline record](./code_constructs/INLINE_RECORD.md)
194195

195196
# Limitations
196197

@@ -201,3 +202,11 @@ The analyzer does not keep track of polymorphic variants, as explained in the
201202

202203
If you have a strong need/desire for this feature, please feel free to
203204
[open an issue](https://github.com/LexiFi/dead_code_analyzer/issues/new)
205+
206+
## Inline record
207+
208+
The analyzer does not keep track of fields in inline records, as explained in
209+
the [Inline record](./code_constructs/INLINE_RECORD.md) example
210+
211+
If you have a strong need/desire for this feature, please feel free to
212+
[open an issue](https://github.com/LexiFi/dead_code_analyzer/issues/new)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
The reference files for this example are in the
2+
[inline\_record](../../../examples/docs/fields_and_constructors/code_constructs/inline_record) directory.
3+
4+
The reference takes place in `/tmp/docs/fields_and_constructors/code_constructs`, which
5+
is a copy of the [code\_constructs](../../../examples/docs/fields_and_constructors/code_constructs)
6+
directory. Reported locations may differ depending on the location of the source
7+
files.
8+
9+
The compilation command is :
10+
```
11+
make -C inline_record build
12+
```
13+
14+
The analysis command is :
15+
```
16+
make -C inline_record analyze
17+
```
18+
19+
The compile + analyze command is :
20+
```
21+
make -C inline_record
22+
```
23+
24+
> [!IMPORTANT]
25+
> **LIMITATION**
26+
>
27+
> The analyzer ignores fields in inline records.
28+
29+
## First run
30+
31+
Code :
32+
```OCaml
33+
(* inline_record_lib.mli *)
34+
type ('a, 'b) t =
35+
| Both of {left : 'a; right : 'b}
36+
| Left of 'a
37+
| Right of 'b
38+
39+
val get_left_opt : ('a, 'b) t -> 'a option
40+
```
41+
```OCaml
42+
(* inline_record_lib.ml *)
43+
type ('a, 'b) t =
44+
| Both of {left : 'a; right : 'b}
45+
| Left of 'a
46+
| Right of 'b
47+
48+
let get_left_opt = function
49+
| Both {left; _}
50+
| Left left -> Some left
51+
| _ -> None
52+
```
53+
```OCaml
54+
(* inline_record_bin.ml *)
55+
let () =
56+
let open Inline_record_lib in
57+
let both = Both {left = 1; right = "one"} in
58+
match get_left_opt both with
59+
| Some left -> assert (left = 1)
60+
| _ -> assert false
61+
```
62+
63+
Before looking at the analysis results, let's look at the code.
64+
65+
The `Inline_record_lib` defines 1 variant type `t` with 3 constructors : `Both`,
66+
`Left`, and `Right`. Only the first one is used, the 2 others are only matched
67+
on in `get_left_opt`. Constructor `Both` has an inline record argument with 2
68+
fields : `left`, and `right`. Only the first one is used, the other one is only
69+
written to.
70+
Following the report semantics on constructors and fields, `Left`, `Right`, and
71+
`right` should be reported unused.
72+
73+
However, the analyzer does not report unused inline record fields.
74+
The compiler also does not warn on unused inline record fields.
75+
76+
Compile and analyze :
77+
```
78+
$ make -C inline_record
79+
make: Entering directory '/tmp/docs/fields_and_constructors/code_constructs/inline_record'
80+
ocamlopt -w +37+69 -bin-annot inline_record_lib.mli inline_record_lib.ml inline_record_bin.ml
81+
dead_code_analyzer --nothing -T all .
82+
Scanning files...
83+
[DONE]
84+
85+
.> UNUSED CONSTRUCTORS/RECORD FIELDS:
86+
====================================
87+
/tmp/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:4: t.Left
88+
/tmp/docs/fields_and_constructors/code_constructs/inline_record/inline_record_lib.mli:5: t.Right
89+
90+
Nothing else to report in this section
91+
--------------------------------------------------------------------------------
92+
93+
94+
make: Leaving directory '/tmp/docs/fields_and_constructors/code_constructs/inline_record'
95+
```
96+
97+
As expected, `t.Left`, and `t.Right` are reported unused by the analyzer.
98+
As explained, the analyzer does not report inline record fields so `right` is
99+
not reported.
100+
Fixing the unused constructors is the same as in the
101+
[Polymorphic type](./POLYMORPHIC_TYPE.md) example. Our work here is done.

0 commit comments

Comments
 (0)