How to make OCaml compiler report unused module aliases?

Hi,

I wonder if it is possible to make the OCaml compiler to report unused module aliases?

For example, in the following file (test.ml), I want to enable ocamlc to report the warnings that two module aliases S and L are unused.

module S = String
module L = List

let foo () =
  let a = 1 in
  let b = 2 in
  a + b
;;

The only relevant warning options that I can find from ocamlc -warn-help are 31 (31 [module-linked-twice] A module is linked twice in the same executable) and 60 ([unused-module] Unused module declaration).

However, when running the following command, ocamlc doesn’t report any warning.

$ ocamlc -w +31+60+66 test.ml

# no warning is reported

Does anyone know how to enable ocamlc to report such warnings that module S = String and module L = List are unused?

Thank you for spending your time to read my question!

Hello,

You need to enable warning 60 and add an empty interface file for it to trigger.

Cheers,
Nicolas

2 Likes

@nojb: Thanks a lot! This is a great tip.

I create an empty file test.mli and now ocamlc can report the unused module aliases:

$ ocamlc -w +60 test.mli test.ml

File "test.ml", line 2, characters 0-15:
2 | module L = List
    ^^^^^^^^^^^^^^^
Warning 60 [unused-module]: unused module L.

@nojb: Do you know if there is an option to enable this warning for the whole project of many files?

I just realized that the above solution requires me to create empty interfaces for all files in my project, and it’s quite laborious…

Btw, I’m using Dune in my build system.

1 Like

Yes, it is enugh to add

(env (_ (flags :standard -w +60)))

in the top-level dune file.

For executables, you can set (executables_implicit_empty_intf true) in your dune-project file to automatically generate an empty interface whenever one does not exist already (see Stanza Reference — Dune documentation).

For libraries, there will be an analogous option in 3.0, see https://github.com/ocaml/dune/pull/4955.

Cheers,
Nicolas

6 Likes

@nojb: Thanks a lot for the update! I was able to use those options with the newest Dune 3.0-alpha built from source code.

FYI, the latest dune 2.9.1 from Opam doesn’t support (executables_implicit_empty_intf true) which I think is the syntax of (lang dune 3.0)