Skip to content

Commit 1cd9baa

Browse files
committed
remove unused modules and types
1 parent 0d7dd0b commit 1cd9baa

File tree

9 files changed

+68
-18
lines changed

9 files changed

+68
-18
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type action_type =
1313
| RewriteIdent of {new_ident: Longident.t}
1414
| PrefixVariableWithUnderscore
1515
| RemoveUnusedVariable
16+
| RemoveUnusedType
17+
| RemoveUnusedModule
1618

1719
(* TODO:
1820
- Unused var in patterns (and aliases )*)
@@ -36,6 +38,8 @@ let action_to_string = function
3638
Printf.sprintf "RewriteIdentToModule(%s)" module_name
3739
| PrefixVariableWithUnderscore -> "PrefixVariableWithUnderscore"
3840
| RemoveUnusedVariable -> "RemoveUnusedVariable"
41+
| RemoveUnusedType -> "RemoveUnusedType"
42+
| RemoveUnusedModule -> "RemoveUnusedModule"
3943
| ReplaceWithVariantConstructor {constructor_name} ->
4044
Printf.sprintf "ReplaceWithVariantConstructor(%s)"
4145
(constructor_name |> Longident.flatten |> String.concat ".")
@@ -69,6 +73,16 @@ let emit_possible_actions_from_warning loc w =
6973
action = RemoveUnusedVariable;
7074
description = "Remove unused variable";
7175
}
76+
| Unused_type_declaration _ ->
77+
add_possible_action
78+
{loc; action = RemoveUnusedType; description = "Remove unused type"}
79+
| Unused_module _ ->
80+
add_possible_action
81+
{loc; action = RemoveUnusedModule; description = "Remove unused module"}
82+
| Unused_pat -> (* TODO: Remove full pattern. *) ()
83+
| Unused_argument ->
84+
(* TODO(actions) Remove unused argument or prefix with underscore *) ()
85+
| Unused_rec_flag -> (* TODO(actions) Remove unused rec flag *) ()
7286
| _ -> ()
7387

7488
let _ =

compiler/ml/env.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,9 +1638,7 @@ and store_type ~check id info env =
16381638
let loc = info.type_loc in
16391639
if check then
16401640
check_usage loc id
1641-
(fun s ->
1642-
(* TODO(actions) Remove unused type *)
1643-
Warnings.Unused_type_declaration s)
1641+
(fun s -> Warnings.Unused_type_declaration s)
16441642
type_declarations;
16451643
let path = Pident id in
16461644
let constructors = Datarepr.constructors_of_type path info in
@@ -1722,11 +1720,7 @@ and store_extension ~check id ext env =
17221720
and store_module ~check id md env =
17231721
let loc = md.md_loc in
17241722
if check then
1725-
check_usage loc id
1726-
(fun s ->
1727-
(* TODO(actions) Remove unused module *)
1728-
Warnings.Unused_module s)
1729-
module_declarations;
1723+
check_usage loc id (fun s -> Warnings.Unused_module s) module_declarations;
17301724

17311725
let deprecated = Builtin_attributes.deprecated_of_attrs md.md_attributes in
17321726
{

compiler/ml/parmatch.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,6 @@ let check_unused pred casel =
22112211
|> List.filter (fun p ->
22122212
not (Variant_type_spread.is_pat_from_variant_spread_attr p))
22132213
|> List.iter (fun p ->
2214-
(* TODO(actions) Remove unused pattern or replace with _ *)
22152214
Location.prerr_warning p.pat_loc Warnings.Unused_pat)
22162215
| Used -> ()
22172216
with Empty | Not_found | NoGuard -> assert false);

compiler/ml/typecore.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,12 +1770,8 @@ let type_pattern ~lev env spat scope expected_ty =
17701770
let pat = type_pat ~allow_existentials:true ~lev new_env spat expected_ty in
17711771
let new_env, unpacks =
17721772
add_pattern_variables !new_env
1773-
~check:(fun s ->
1774-
(* TODO(actions) Remove unused variable or prefix with underscore *)
1775-
Warnings.Unused_var_strict s)
1776-
~check_as:(fun s ->
1777-
(* TODO(actions) Remove unused variable or prefix with underscore *)
1778-
Warnings.Unused_var s)
1773+
~check:(fun s -> Warnings.Unused_var_strict s)
1774+
~check_as:(fun s -> Warnings.Unused_var s)
17791775
in
17801776
(pat, new_env, get_ref pattern_force, unpacks)
17811777
@@ -3661,7 +3657,6 @@ and type_application ~context total_app env funct (sargs : sargs) :
36613657
so the function type including arity can be inferred. *)
36623658
let t1 = newvar () and t2 = newvar () in
36633659
if ty_fun.level >= t1.level && not_identity funct.exp_desc then
3664-
(* TODO(actions) Remove unused argument or prefix with underscore *)
36653660
Location.prerr_warning sarg1.pexp_loc Warnings.Unused_argument;
36663661
unify env ty_fun
36673662
(newty
@@ -4227,7 +4222,6 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
42274222
let {pvb_pat; pvb_attributes} = List.hd spat_sexp_list in
42284223
(* See PR#6677 *)
42294224
Builtin_attributes.warning_scope ~ppwarning:false pvb_attributes (fun () ->
4230-
(* TODO(actions) Remove unused rec flag *)
42314225
Location.prerr_warning pvb_pat.ppat_loc Warnings.Unused_rec_flag));
42324226
List.iter2
42334227
(fun pat (attrs, exp) ->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// actionFilter=RemoveUnusedModule
2+
module M: {} = {}
3+
4+
/* === AVAILABLE ACTIONS:
5+
- RemoveUnusedModule - Remove unused module
6+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// actionFilter=RemoveUnusedType
2+
module M: {} = {}
3+
4+
/* === AVAILABLE ACTIONS:
5+
- RemoveUnusedType - Remove unused type
6+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// actionFilter=RemoveUnusedModule
2+
module M: {} = {
3+
module N = {
4+
let x = 12
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// actionFilter=RemoveUnusedType
2+
module M: {} = {
3+
type t = int
4+
}

tools/src/tools.ml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,31 @@ module Actions = struct
13161316
match remove_open_action with
13171317
| Some _ -> None
13181318
| None -> Some str_item)
1319+
| Pstr_type (_, _type_declarations) -> (
1320+
let remove_unused_type_action =
1321+
actions
1322+
|> List.find_opt
1323+
(fun (action : Cmt_utils.cmt_action) ->
1324+
match action.action with
1325+
| RemoveUnusedType ->
1326+
action.loc = str_item.pstr_loc
1327+
| _ -> false)
1328+
in
1329+
match remove_unused_type_action with
1330+
| Some _ -> None
1331+
| None -> Some str_item)
1332+
| Pstr_module {pmb_loc} ->
1333+
let remove_unused_module_action_locs =
1334+
List.filter_map
1335+
(fun (action : Cmt_utils.cmt_action) ->
1336+
match action.action with
1337+
| RemoveUnusedModule -> Some action.loc
1338+
| _ -> None)
1339+
actions
1340+
in
1341+
if List.mem pmb_loc remove_unused_module_action_locs then
1342+
None
1343+
else Some str_item
13191344
| _ -> Some str_item)
13201345
in
13211346
let items = Ast_mapper.default_mapper.structure mapper items in
@@ -1606,7 +1631,9 @@ module Actions = struct
16061631
| PrefixVariableWithUnderscore ->
16071632
List.mem "PrefixVariableWithUnderscore" filter
16081633
| RemoveUnusedVariable ->
1609-
List.mem "RemoveUnusedVariable" filter)
1634+
List.mem "RemoveUnusedVariable" filter
1635+
| RemoveUnusedType -> List.mem "RemoveUnusedType" filter
1636+
| RemoveUnusedModule -> List.mem "RemoveUnusedModule" filter)
16101637
in
16111638
match applyActionsToFile path possible_actions with
16121639
| Ok applied ->

0 commit comments

Comments
 (0)