Skip to content

Commit

Permalink
feat: Enhance mutability checks
Browse files Browse the repository at this point in the history
Signed-off-by: Kakadu <Kakadu@pm.me>
  • Loading branch information
Kakadu committed Oct 1, 2023
1 parent 465abf7 commit 79be8b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- #15: Split 'string_concat' lint to check separately patterns 'a^b^c' (level=Allow) and 'List.fold_left (^)' (level=Warn).
(reported by @edwintorok)
- #16: Calculate test coverage.
- The lint 'mutable_hashtables' now check for mutability in general: references, mutable record fields, etc.


## 1.0.0 (24-03-2023)
Expand Down
17 changes: 17 additions & 0 deletions src/typed/Hashtables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type input = Tast_iterator.iterator

let lint_source = LINT.FPCourse
let lint_id = "mutable_hashtables"

(* TODO: rename that it checks mutability in general *)
let level = LINT.Warn

let documentation =
Expand Down Expand Up @@ -95,6 +97,21 @@ let run _ fallback =
let loc = typ.Typedtree.ctyp_loc in
check loc pat_typ typ;
fallback.typ self typ)
; type_declaration =
(fun self td ->
fallback.type_declaration self td;
match td.Typedtree.typ_type.Types.type_kind with
| Types.Type_abstract | Type_open -> ()
| Type_record (labels, _) ->
List.iter labels ~f:(function
| { ld_mutable = Mutable; ld_loc = loc; _ } ->
CollectedLints.add
~loc
(report loc.Location.loc_start.Lexing.pos_fname ~loc ())
| _ -> ())
| Type_variant _ ->
(* TODO(Kakadu): Algbraic constuctors could have mutable record arguments *)
())
; expr =
(fun self expr ->
let loc = expr.Typedtree.exp_loc in
Expand Down
5 changes: 5 additions & 0 deletions tests/typed/hashtables.t/Hashtables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ let get_unique_identity_code () =
let code = !last_identity in
last_identity := code + 1;
code

module BindsMap = Map.Make(String)
type env = int option ref BindsMap.t

type args = { mutable count: int }
8 changes: 8 additions & 0 deletions tests/typed/hashtables.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
18 | last_identity := code + 1;
^^^^^^^^^^^^^
Alert zanuda-linter: Using mutable data structures for teaching purposes is usually discouraged. Replace Hashtables by standart tree-like maps or consider Hash-Array Mapped Tries (HAMT). Use mutable `ref`erences and mutable structure fields only if it is really required. In all places where it is needed indeed, describe in a comment why it is needed there.
File "Hashtables.ml", line 22, characters 11-25:
22 | type env = int option ref BindsMap.t
^^^^^^^^^^^^^^
Alert zanuda-linter: Using mutable data structures for teaching purposes is usually discouraged. Replace Hashtables by standart tree-like maps or consider Hash-Array Mapped Tries (HAMT). Use mutable `ref`erences and mutable structure fields only if it is really required. In all places where it is needed indeed, describe in a comment why it is needed there.
File "Hashtables.ml", line 24, characters 14-32:
24 | type args = { mutable count: int }
^^^^^^^^^^^^^^^^^^
Alert zanuda-linter: Using mutable data structures for teaching purposes is usually discouraged. Replace Hashtables by standart tree-like maps or consider Hash-Array Mapped Tries (HAMT). Use mutable `ref`erences and mutable structure fields only if it is really required. In all places where it is needed indeed, describe in a comment why it is needed there.

0 comments on commit 79be8b5

Please sign in to comment.