Skip to content

Commit 17709f7

Browse files
committed
prefix unused
1 parent a4a9b25 commit 17709f7

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type action_type =
1010
| RewriteObjectToRecord
1111
| RewriteArrayToTuple
1212
| RewriteIdent of {new_ident: Longident.t}
13+
| PrefixVariableWithUnderscore
14+
| RemoveUnusedVariable
1315

1416
type cmt_action = {loc: Location.t; action: action_type; description: string}
1517

@@ -23,6 +25,19 @@ let emit_possible_actions_from_warning loc w =
2325
| Unused_match | Unreachable_case ->
2426
add_possible_action
2527
{loc; action = RemoveSwitchCase; description = "Remove switch case"}
28+
| Unused_var _ | Unused_var_strict _ ->
29+
add_possible_action
30+
{
31+
loc;
32+
action = PrefixVariableWithUnderscore;
33+
description = "Prefix with `_`";
34+
};
35+
add_possible_action
36+
{
37+
loc;
38+
action = RemoveUnusedVariable;
39+
description = "Remove unused variable";
40+
}
2641
| _ -> ()
2742

2843
let _ =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let f = () => {
2+
let _x = 1
3+
12
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let f = () => {
2+
let x = 1
3+
12
4+
}

tools/src/tools.ml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,33 @@ module Actions = struct
13191319
| _ -> Some str_item)
13201320
in
13211321
Ast_mapper.default_mapper.structure mapper items);
1322+
value_bindings =
1323+
(fun mapper bindings ->
1324+
(* TODO: Implement removing binding action *)
1325+
Ast_mapper.default_mapper.value_bindings mapper bindings);
1326+
pat =
1327+
(fun mapper pattern ->
1328+
let pattern =
1329+
match pattern.ppat_desc with
1330+
| Ppat_var var -> (
1331+
let prefix_underscore_action =
1332+
actions
1333+
|> List.find_opt (fun (action : Cmt_utils.cmt_action) ->
1334+
match action.action with
1335+
| PrefixVariableWithUnderscore ->
1336+
action.loc = pattern.ppat_loc
1337+
| _ -> false)
1338+
in
1339+
match prefix_underscore_action with
1340+
| Some _ ->
1341+
{
1342+
pattern with
1343+
ppat_desc = Ppat_var {var with txt = "_" ^ var.txt};
1344+
}
1345+
| None -> pattern)
1346+
| _ -> pattern
1347+
in
1348+
Ast_mapper.default_mapper.pat mapper pattern);
13221349
cases =
13231350
(fun mapper cases ->
13241351
let cases =

0 commit comments

Comments
 (0)