How to make OCaml compiler report unused functions?

人走茶凉 提交于 2020-05-30 04:25:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!