Skip to content

Commit 3ca2a95

Browse files
committed
remove unused rec flag
1 parent 870d0b5 commit 3ca2a95

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

compiler/ext/warnings.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,7 @@ let message = function
397397
tc1 tc2
398398
| Unused_value_declaration v -> "unused value " ^ v ^ "."
399399
| Unused_open s -> "unused open " ^ s ^ "."
400-
| Unused_type_declaration s ->
401-
(* TODO(actions) Remove type declaration *)
402-
"unused type " ^ s ^ "."
400+
| Unused_type_declaration s -> "unused type " ^ s ^ "."
403401
| Unused_for_index s -> "unused for-loop index " ^ s ^ "."
404402
| Unused_constructor (s, false, false) ->
405403
(* TODO(actions) Remove constructor *)
@@ -424,9 +422,7 @@ let message = function
424422
name
425423
^ " is never used to build values.\n\
426424
It is exported or rebound as a private extension.")
427-
| Unused_rec_flag ->
428-
(* TODO(actions) Remove rec flag *)
429-
"unused rec flag."
425+
| Unused_rec_flag -> "unused rec flag."
430426
| Ambiguous_name ([s], tl, false) ->
431427
s ^ " belongs to several types: " ^ String.concat " " tl
432428
^ "\nThe first one was selected. Please disambiguate if this is wrong."

compiler/ml/cmt_utils.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type action_type =
1515
| RemoveUnusedVariable
1616
| RemoveUnusedType
1717
| RemoveUnusedModule
18+
| RemoveRecFlag
1819

1920
(* TODO:
2021
- Unused var in patterns (and aliases )*)
@@ -49,6 +50,7 @@ let action_to_string = function
4950
| RewriteIdent {new_ident} ->
5051
Printf.sprintf "RewriteIdent(%s)"
5152
(Longident.flatten new_ident |> String.concat ".")
53+
| RemoveRecFlag -> "RemoveRecFlag"
5254

5355
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
5456
let add_possible_action action = !_add_possible_action action
@@ -82,7 +84,9 @@ let emit_possible_actions_from_warning loc w =
8284
| Unused_pat -> (* TODO: Remove full pattern. *) ()
8385
| Unused_argument ->
8486
(* TODO(actions) Remove unused argument or prefix with underscore *) ()
85-
| Unused_rec_flag -> (* TODO(actions) Remove unused rec flag *) ()
87+
| Unused_rec_flag ->
88+
add_possible_action
89+
{loc; action = RemoveRecFlag; description = "Remove rec flag"}
8690
| _ -> ()
8791

8892
let _ =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// actionFilter=RemoveRecFlag
2+
let f = 12
3+
let fn = () => {
4+
let x = 12
5+
}
6+
7+
/* === AVAILABLE ACTIONS:
8+
- RemoveRecFlag - Remove rec flag
9+
- RemoveRecFlag - Remove rec flag
10+
*/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// actionFilter=RemoveRecFlag
2+
let rec f = 12
3+
let fn = () => {
4+
let rec x = 12
5+
}

tools/src/tools.ml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,24 @@ module Actions = struct
12981298
let mapper =
12991299
{
13001300
Ast_mapper.default_mapper with
1301+
structure_item =
1302+
(fun mapper str_item ->
1303+
let remove_rec_flag_action_locs =
1304+
List.filter_map
1305+
(fun (action : Cmt_utils.cmt_action) ->
1306+
match action.action with
1307+
| RemoveRecFlag -> Some action.loc
1308+
| _ -> None)
1309+
actions
1310+
in
1311+
match str_item.pstr_desc with
1312+
| Pstr_value (Recursive, ({pvb_pat = {ppat_loc}} :: _ as bindings))
1313+
when List.mem ppat_loc remove_rec_flag_action_locs ->
1314+
let str_item =
1315+
Ast_mapper.default_mapper.structure_item mapper str_item
1316+
in
1317+
{str_item with pstr_desc = Pstr_value (Nonrecursive, bindings)}
1318+
| _ -> Ast_mapper.default_mapper.structure_item mapper str_item);
13011319
structure =
13021320
(fun mapper items ->
13031321
let items =
@@ -1518,6 +1536,17 @@ module Actions = struct
15181536
else
15191537
(* Other cases when the loc is on something else in the expr *)
15201538
match (expr.pexp_desc, action.action) with
1539+
| ( Pexp_let
1540+
( Recursive,
1541+
({pvb_pat = {ppat_loc}} :: _ as bindings),
1542+
cont ),
1543+
RemoveRecFlag )
1544+
when action.loc = ppat_loc ->
1545+
Some
1546+
{
1547+
expr with
1548+
pexp_desc = Pexp_let (Nonrecursive, bindings, cont);
1549+
}
15211550
| ( Pexp_field
15221551
( {pexp_desc = Pexp_ident e},
15231552
{txt = Lident inner; loc} ),
@@ -1633,7 +1662,8 @@ module Actions = struct
16331662
| RemoveUnusedVariable ->
16341663
List.mem "RemoveUnusedVariable" filter
16351664
| RemoveUnusedType -> List.mem "RemoveUnusedType" filter
1636-
| RemoveUnusedModule -> List.mem "RemoveUnusedModule" filter)
1665+
| RemoveUnusedModule -> List.mem "RemoveUnusedModule" filter
1666+
| RemoveRecFlag -> List.mem "RemoveRecFlag" filter)
16371667
in
16381668
match applyActionsToFile path possible_actions with
16391669
| Ok applied ->

0 commit comments

Comments
 (0)