|
| 1 | +The reference files for this example are in the |
| 2 | +[coercion](../../../examples/docs/methods/code_constructs/coercion) directory. |
| 3 | + |
| 4 | +The reference takes place in `/tmp/docs/methods/code_constructs`, which |
| 5 | +is a copy of the [code\_constructs](../../../examples/docs/methods/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 coercion build |
| 12 | +``` |
| 13 | + |
| 14 | +The analysis command is : |
| 15 | +``` |
| 16 | +make -C coercion analyze |
| 17 | +``` |
| 18 | + |
| 19 | +The compile + analyze command is : |
| 20 | +``` |
| 21 | +make -C coercion |
| 22 | +``` |
| 23 | + |
| 24 | +## First run |
| 25 | + |
| 26 | +Code: |
| 27 | +```OCaml |
| 28 | +(* coercion_lib.mli *) |
| 29 | +val obj : < |
| 30 | + used_by_requirement : unit; |
| 31 | + unused : unit; |
| 32 | +> |
| 33 | +``` |
| 34 | +```OCaml |
| 35 | +(* coercion_lib.ml *) |
| 36 | +let obj = object |
| 37 | + method used_by_requirement = () |
| 38 | + method unused = () |
| 39 | +end |
| 40 | +``` |
| 41 | +```OCaml |
| 42 | +(* coercion_bin.ml *) |
| 43 | +open Coercion_lib |
| 44 | +
|
| 45 | +let () = |
| 46 | + let _coerce = (obj :> < used_by_requirement : unit >) in |
| 47 | + () |
| 48 | +``` |
| 49 | + |
| 50 | +Before looking at the analysis results, let's look at the code. |
| 51 | + |
| 52 | +There is 1 object : `Coercion_lib.obj`, which defined 2 zmthods : |
| 53 | +`used_by_requirement`, and `unused`. Neither of them is explicitly referenced, |
| 54 | +and the object is only manipulated through a coercion. The coercion produces an |
| 55 | +alias to `obj` named `_coerce`, which only exposes the method |
| 56 | +`used_by_requirement`. Because the method is required to exists in `obj` for the |
| 57 | +coercion, then the analyzer effectively considers it as used by requirement. |
| 58 | + |
| 59 | +Compile and analyze: |
| 60 | +``` |
| 61 | +$ make -C coercion |
| 62 | +make: Entering directory '/tmp/docs/methods/code_constructs/coercion' |
| 63 | +ocamlopt -bin-annot coercion_lib.mli coercion_lib.ml coercion_bin.ml |
| 64 | +dead_code_analyzer --nothing -M all . |
| 65 | +Scanning files... |
| 66 | + [DONE] |
| 67 | +
|
| 68 | +.> UNUSED METHODS: |
| 69 | +================= |
| 70 | +/tmp/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused |
| 71 | +
|
| 72 | +Nothing else to report in this section |
| 73 | +-------------------------------------------------------------------------------- |
| 74 | +
|
| 75 | +
|
| 76 | +make: Leaving directory '/tmp/docs/methods/code_constructs/coercion' |
| 77 | +``` |
| 78 | + |
| 79 | +As expected, the anlyzer reports `obj#unsed` as unused and not |
| 80 | +`obj#used_by_requirement`. The unused method can be removed from the `.mli` and |
| 81 | +the `.ml`. Our work here is done. |
0 commit comments