How to make OCaml compiler report unused functions?

620 views Asked by At

I wonder if is there any ways to make OCaml compiler report warnings about unused functions? I googled but there are not much topics discussed about this feature.

In particular, in the following program, there are two functions "foo" and "bar" which are declared but "bar" is not used in the "_" function. So I think that the OCaml compiler should report "bar" as an unused function.

let foo x y = x + y

let bar x y z = x + y + z         (* should be reported unused *)

let _ =
  let x = foo 1 2 in
  x
2

There are 2 answers

3
Thomas Leonard On

You need to define a (possibly-empty) .mli interface file saying what this module exports. Otherwise, you're just defining a bar function for other modules to use.

(and make sure you're compiling with warnings on, of course)

0
Alain Frisch On

You can have a look at https://github.com/alainfrisch/dead_code_analyzer , which is a "global" dead code detector. It collects from .cmi files the set of exported values and from .cmt files the set of external references, thus allowing to detect exported values which are never used. (There is also some logic to analyze optional arguments and report which ones are never or always passed.)