ocaml.lang.best-practice.hashtbl.hashtbl-find-outside-try

Author
4,866
Download Count*
License
You should not use Hashtbl.find outside of a try, or you should use Hashtbl.find_opt
Run Locally
Run in CI
Defintion
rules:
- id: hashtbl-find-outside-try
patterns:
- pattern: |
Hashtbl.find ...
- pattern-not-inside: |
try ... with ... -> ...
- pattern-not-inside: |
match ... with | ... -> ...
message: You should not use Hashtbl.find outside of a try, or you should use
Hashtbl.find_opt
languages:
- ocaml
severity: WARNING
metadata:
category: best-practice
technology:
- ocaml
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
Examples
hashtbl.ml
let test1 xs =
(* ruleid:hashtbl-find-outside-try *)
if Hashtbl.find h 1
then 1
else 2
let test2 xs =
(* ok *)
try
if Hashtbl.find h 1
then 1
else 2
with Not_found -> 3
let test3 xs =
(* ok *)
match Hashtbl.find h 1 with
| true -> 1
| false -> 2
| exception Not_found -> 3
Short Link: https://sg.run/yd7A