问题
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
回答1:
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)
回答2:
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.)
来源:https://stackoverflow.com/questions/30886350/how-to-make-ocaml-compiler-report-unused-functions