diff --git a/check/classic/classic.exp b/check/classic/classic.exp index 8215e7a1..6ed8bc5b 100644 --- a/check/classic/classic.exp +++ b/check/classic/classic.exp @@ -179,6 +179,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -197,8 +199,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused - ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m diff --git a/check/classic/classic.ref b/check/classic/classic.ref index 3bf7f69e..8b347601 100644 --- a/check/classic/classic.ref +++ b/check/classic/classic.ref @@ -179,6 +179,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -197,8 +199,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#unused @@ -724,7 +724,7 @@ Nothing else to report in this section -------------------------------------------------------------------------------- -Total: 597 +Total: 596 Success: 594 -Failed: 3 -Ratio: 99.4974874372% +Failed: 2 +Ratio: 99.6644295302% diff --git a/check/internal/internal.exp b/check/internal/internal.exp index 435ce050..bc2224ab 100644 --- a/check/internal/internal.exp +++ b/check/internal/internal.exp @@ -131,6 +131,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -149,8 +151,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused - ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m diff --git a/check/internal/internal.ref b/check/internal/internal.ref index 526d049b..c7fe82d3 100644 --- a/check/internal/internal.ref +++ b/check/internal/internal.ref @@ -131,6 +131,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -149,8 +151,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#unused @@ -676,7 +676,7 @@ Nothing else to report in this section -------------------------------------------------------------------------------- -Total: 553 +Total: 552 Success: 550 -Failed: 3 -Ratio: 99.4575045208% +Failed: 2 +Ratio: 99.6376811594% diff --git a/check/threshold-1/threshold-1.exp b/check/threshold-1/threshold-1.exp index eba5339d..71a3e827 100644 --- a/check/threshold-1/threshold-1.exp +++ b/check/threshold-1/threshold-1.exp @@ -185,6 +185,8 @@ ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly +./examples/docs/methods/code_constructs/alias/alias_lib.mli:8: alias + ./examples/docs/methods/code_constructs/class/class_bin.ml:4: push_n_times ./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times @@ -201,8 +203,6 @@ ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times -./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias - ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:3: min @@ -422,6 +422,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -440,8 +442,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused - ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m @@ -505,6 +505,9 @@ Nothing else to report in this section .>-> ALMOST UNUSED METHODS: Called 1 time(s): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used_by_alias + ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#peek ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push @@ -530,9 +533,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias - ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#internally_used ./examples/using_dune/preprocessed_lib/preprocessed_no_intf.ml:12: immediate#externally_used diff --git a/check/threshold-1/threshold-1.ref b/check/threshold-1/threshold-1.ref index fc1e5e1d..1808d140 100644 --- a/check/threshold-1/threshold-1.ref +++ b/check/threshold-1/threshold-1.ref @@ -185,6 +185,8 @@ ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly +./examples/docs/methods/code_constructs/alias/alias_lib.mli:8: alias + ./examples/docs/methods/code_constructs/class/class_bin.ml:4: push_n_times ./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times @@ -201,8 +203,6 @@ ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times -./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias - ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:3: min @@ -422,6 +422,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -440,8 +442,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#unused @@ -504,6 +504,9 @@ Nothing else to report in this section .>-> ALMOST UNUSED METHODS: Called 1 time(s): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used_by_alias + ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#peek ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push @@ -529,8 +532,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#internally_used ./examples/using_dune/preprocessed_lib/preprocessed_no_intf.ml:12: immediate#externally_used @@ -1140,7 +1141,7 @@ Nothing else to report in this section -------------------------------------------------------------------------------- -Total: 938 -Success: 934 -Failed: 4 -Ratio: 99.5735607676% +Total: 937 +Success: 935 +Failed: 2 +Ratio: 99.7865528282% diff --git a/check/threshold-3-0.5/threshold-3-0.5.exp b/check/threshold-3-0.5/threshold-3-0.5.exp index 40fbd96a..5a25dad4 100644 --- a/check/threshold-3-0.5/threshold-3-0.5.exp +++ b/check/threshold-3-0.5/threshold-3-0.5.exp @@ -185,6 +185,8 @@ ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly +./examples/docs/methods/code_constructs/alias/alias_lib.mli:8: alias + ./examples/docs/methods/code_constructs/class/class_bin.ml:4: push_n_times ./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times @@ -201,8 +203,6 @@ ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times -./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias - ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:3: min @@ -424,7 +424,7 @@ ./examples/docs/exported_values/hello_world/hello_world_with_intf.mli:4: world ./examples/docs/exported_values/hello_world/hello_world_without_intf.ml:4: world -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original ./examples/docs/optional_arguments/code_constructs/intext_app/intext_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/intext_app/intext_app_lib.mli:3: min @@ -565,6 +565,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -583,8 +585,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused - ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m @@ -648,6 +648,9 @@ Nothing else to report in this section .>-> ALMOST UNUSED METHODS: Called 1 time(s): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used_by_alias + ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#peek ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push @@ -673,9 +676,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias - ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#internally_used ./examples/using_dune/preprocessed_lib/preprocessed_no_intf.ml:12: immediate#externally_used diff --git a/check/threshold-3-0.5/threshold-3-0.5.ref b/check/threshold-3-0.5/threshold-3-0.5.ref index 887e94e7..f44dd000 100644 --- a/check/threshold-3-0.5/threshold-3-0.5.ref +++ b/check/threshold-3-0.5/threshold-3-0.5.ref @@ -185,6 +185,8 @@ ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:4: poly_of_int ./examples/docs/fields_and_constructors/code_constructs/polymorphic_variant/polymorphic_variant_lib.mli:6: float_opt_of_poly +./examples/docs/methods/code_constructs/alias/alias_lib.mli:8: alias + ./examples/docs/methods/code_constructs/class/class_bin.ml:4: push_n_times ./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times @@ -201,8 +203,6 @@ ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_bin.ml:2: push_n_times -./examples/docs/methods/limitations/alias/alias_lib.mli:8: alias - ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/external_app/external_app_lib.mli:3: min @@ -424,7 +424,7 @@ ./examples/docs/exported_values/hello_world/hello_world_with_intf.mli:4: world ./examples/docs/exported_values/hello_world/hello_world_without_intf.ml:4: world -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original ./examples/docs/optional_arguments/code_constructs/intext_app/intext_app_lib.mli:2: max ./examples/docs/optional_arguments/code_constructs/intext_app/intext_app_lib.mli:3: min @@ -565,6 +565,8 @@ Nothing else to report in this section .> UNUSED METHODS: ================= +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + ./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset @@ -583,8 +585,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#reset -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#unused -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Should not be detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:2: factory_with_intermediate_binding#m: Not detected ./examples/docs/methods/limitations/factory_fun_indir/factory_fun_indir.ml:8: random_factory#m: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#unused @@ -647,6 +647,9 @@ Nothing else to report in this section .>-> ALMOST UNUSED METHODS: Called 1 time(s): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used +./examples/docs/methods/code_constructs/alias/alias_lib.mli:2: original#used_by_alias + ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#peek ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop ./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push @@ -672,8 +675,6 @@ Nothing else to report in this section ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#pop ./examples/docs/methods/code_constructs/polymorphic_class/polymorphic_class_lib.mli:2: stack#push -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used -./examples/docs/methods/limitations/alias/alias_lib.mli:2: original#used_by_alias: Not detected ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#externally_used ./examples/using_dune/preprocessed_lib/preprocessed.mli:6: immediate#internally_used ./examples/using_dune/preprocessed_lib/preprocessed_no_intf.ml:12: immediate#externally_used @@ -1544,7 +1545,7 @@ Nothing else to report in this section -------------------------------------------------------------------------------- -Total: 1260 -Success: 1256 -Failed: 4 -Ratio: 99.6825396825% +Total: 1259 +Success: 1257 +Failed: 2 +Ratio: 99.8411437649% diff --git a/docs/methods/METHODS.md b/docs/methods/METHODS.md index b2516ab9..dedd4926 100644 --- a/docs/methods/METHODS.md +++ b/docs/methods/METHODS.md @@ -9,7 +9,6 @@ + [Limitations](#limitations) + [Class type](#class-type) + [Object type](#object-type) - + [Alias](#alias) + [Factory function](#factory-function) # Methods @@ -153,6 +152,7 @@ from the `.mli` if there is one and the `.ml`. - [Factory function](./code_constructs/FACTORY_FUN.md) - [Object type](./code_constructs/OBJECT_TYPE.md) - [Coercion](./code_constructs/COERCION.md) + + [Alias](./code_constructus/ALIAS.md) # Limitations @@ -168,16 +168,6 @@ As explained in the [Object type](./code_constructs/OBJECT_TYPE.md) example, the analyzer is currently restricted to not reporting methods declared in object types. -## Alias - -Related issue : -[issue #66](https://github.com/LexiFi/dead_code_analyzer/issues/66). - -In the presence of multiple bindings to the same object, the analyzer corrctly -avoids tracking their methods individually. However, it fails at unifying them -and only keeps track of the methods used through the original binding, where the -methods are defined. This leads to **false positives**. - ### Example The reference files for this example are in the diff --git a/docs/methods/code_constructs/ALIAS.md b/docs/methods/code_constructs/ALIAS.md new file mode 100644 index 00000000..4377db5d --- /dev/null +++ b/docs/methods/code_constructs/ALIAS.md @@ -0,0 +1,184 @@ +The reference files for this example are in the +[alias](../../../examples/docs/methods/code_constructs/alias) directory. + +The reference takes place in `/tmp/docs/methods/code_constructs`, which +is a copy of the [code\_constructs](../../../examples/docs/methods/code_constructs) +directory. Reported locations may differ depending on the location of the source +files. + +The compilation command is : +``` +make -C alias build +``` + +The analysis command is : +``` +make -C alias analyze +``` + +The compile + analyze command is : +``` +make -C alias +``` + +## First run + +Code: +```OCaml +(* alias_lib.mli *) +val original : + < used : unit + ; used_by_alias : unit + ; unused : unit + > + +val alias : + < used : unit + ; used_by_alias : unit + ; unused : unit + > +``` +```OCaml +(* alias_lib.ml *) +let original = + object + method used = () + method used_by_alias = () + method unused = () + end + +let alias = original +``` +```OCaml +(* alias_bin.ml *) +open Alias_lib + +let () = + original#used; + alias#used_by_alias +``` + +Before looking at the analysis results, let's look at the code. + +The `Alias_lib` declares and exports 2 objects : `original`, and `alias`. +They both have the same type, and `alias` is actually defined as equal to +`original`. Thus, `alias` is an alias for `original`. +Their methods are used in `Alias_bin`. The method `used` is used via +`original`, and `used_by_alias` is via `alias`. +Because `alias` is the same object as `original`, the 2 methods are actually +used for both objects. + +Compile and analyze: +``` +$ make -C alias +make: Entering directory '/tmp/docs/methods/code_constructs/alias' +ocamlopt -bin-annot alias_lib.mli alias_lib.ml alias_bin.ml +dead_code_analyzer --nothing -M all . +Scanning files... + [DONE] + +.> UNUSED METHODS: +================= +/tmp/docs/methods/code_constructs/alias/alias_lib.mli:2: original#unused + +Nothing else to report in this section +-------------------------------------------------------------------------------- + + +make: Leaving directory '/tmp/docs/methods/code_constructs/alias' +``` + +As expected, only the method `unused` is reported. However, it is only +reported for `original`. +Due to the dynamic nature of objects, the analyzer reports the methods of the +objects that actually define them. Thus, because `alias` is an alias for +`original`, it does not define any new method, and the analyzer would not +report anything for it. + +## Removing the unused methods + +If we only remove `original#unused` from `alias_lib.ml` and `alias_lib.mli`, +we get the following error: +``` +Error: The implementation alias_lib.ml + does not match the interface alias_lib.mli: + Values do not match: + val alias : < used : unit; used_by_alias : unit > + is not included in + val alias : < unused : unit; used : unit; used_by_alias : unit > + The type < used : unit; used_by_alias : unit > + is not compatible with the type + < unused : unit; used : unit; used_by_alias : unit > + Type < > is not compatible with type < unused : unit > + File "alias_lib.mli", lines 7-11, characters 0-3: Expected declaration + File "alias_lib.ml", line 8, characters 4-9: Actual declaration +``` + +This is because the .mli still exports `alias#unused`, although it is not +defined. The easy fix is to also remove `alias#unused` from the .mli. + +> [!NOTE] +> Alternatively, if we wanted to keep `alias#unused`, we could swap the +> definitions, making `original` the alias (with a coercion to discard +> `unused` : `let original = (alias :> )`). +> The analyzer would then report `alias#unused` as unused. +> +> We could also define `alias` as a new object with methods `used` and +> `used_by_alias` equal to those of `original`. Then, `alias` would not +> longer be an alias for `original` and their methods would be tracked +> and reported independently. Thus, the analyzer would report `alias#unused` +> , and `alias#used`. It would not report any method for `original` because +> they are all used by `alias`. + +Code: +```OCaml +(* alias_lib.mli *) +val original : + < used : unit + ; used_by_alias : unit + > + +val alias : + < used : unit + ; used_by_alias : unit + > +``` +```OCaml +(* alias_lib.ml *) +let original = + object + method used = () + method used_by_alias = () + end + +let alias = original +``` +```OCaml +(* alias_bin.ml *) +open Alias_lib + +let () = + original#used; + alias#used_by_alias +``` + +Compile and analyze: +``` +$ make -C alias +make: Entering directory '/tmp/docs/methods/code_constructs/alias' +ocamlopt -bin-annot alias_lib.mli alias_lib.ml alias_bin.ml +dead_code_analyzer --nothing -M all . +Scanning files... + [DONE] + +.> UNUSED METHODS: +================= + +Nothing else to report in this section +-------------------------------------------------------------------------------- + + +make: Leaving directory '/tmp/docs/methods/code_constructs/alias' +``` + +There is no more unused method. Our work here is done. diff --git a/examples/docs/methods/code_constructs/Makefile b/examples/docs/methods/code_constructs/Makefile index 33cff97b..4411c077 100644 --- a/examples/docs/methods/code_constructs/Makefile +++ b/examples/docs/methods/code_constructs/Makefile @@ -12,6 +12,7 @@ build: make -C factory_fun build make -C object_type build make -C coercion build + make -C alias build clean: rm -f *~ *.cm* *.o *.obj @@ -24,4 +25,5 @@ clean: make -C factory_fun clean make -C object_type clean make -C coercion clean + make -C alias clean diff --git a/examples/docs/methods/limitations/alias/Makefile b/examples/docs/methods/code_constructs/alias/Makefile similarity index 100% rename from examples/docs/methods/limitations/alias/Makefile rename to examples/docs/methods/code_constructs/alias/Makefile diff --git a/examples/docs/methods/limitations/alias/alias_bin.ml b/examples/docs/methods/code_constructs/alias/alias_bin.ml similarity index 100% rename from examples/docs/methods/limitations/alias/alias_bin.ml rename to examples/docs/methods/code_constructs/alias/alias_bin.ml diff --git a/examples/docs/methods/limitations/alias/alias_lib.ml b/examples/docs/methods/code_constructs/alias/alias_lib.ml similarity index 100% rename from examples/docs/methods/limitations/alias/alias_lib.ml rename to examples/docs/methods/code_constructs/alias/alias_lib.ml diff --git a/examples/docs/methods/limitations/alias/alias_lib.mli b/examples/docs/methods/code_constructs/alias/alias_lib.mli similarity index 100% rename from examples/docs/methods/limitations/alias/alias_lib.mli rename to examples/docs/methods/code_constructs/alias/alias_lib.mli diff --git a/examples/docs/methods/limitations/Makefile b/examples/docs/methods/limitations/Makefile index 1123cd42..82966a2b 100644 --- a/examples/docs/methods/limitations/Makefile +++ b/examples/docs/methods/limitations/Makefile @@ -3,11 +3,9 @@ all: build build: - make -C alias build make -C factory_fun_indir build clean: rm -f *~ *.cm* *.o *.obj - make -C alias clean make -C factory_fun_indir clean diff --git a/src/deadCode.ml b/src/deadCode.ml index 6181baf2..3b1e7123 100644 --- a/src/deadCode.ml +++ b/src/deadCode.ml @@ -76,7 +76,8 @@ let value_binding super self x = _}; _ } -> - VdNode.merge_locs loc1 loc2 + VdNode.merge_locs loc1 loc2; + DeadObj.add_equal loc1 loc2 | { vb_pat = { pat_desc = Tpat_var ( _,