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

profile photo of semgrepsemgrep
Author
4,866
Download Count*

You should not use List.find outside of a try, or you should use List.find_opt

Run Locally

Run in CI

Defintion

rules:
  - id: list-find-outside-try
    patterns:
      - pattern: |
          List.find ...
      - pattern-not-inside: |
          try ... with ... -> ...
    message: You should not use List.find outside of a try, or you should use
      List.find_opt
    languages:
      - ocaml
    severity: WARNING
    metadata:
      category: best-practice
      technology:
        - ocaml
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

list.ml

let test1 xs =
  (* ruleid:list-find-outside-try *)
  if List.find 1 xs
  then 1
  else 2

let test2 xs =
 (* ok *)
 try
   if List.find 1 xs
   then 1
   else 2
 with Not_found -> 3