Subject: Re: bug#61266: [PATCH 1/1] lint: Add unused-modules linter.

From: Ludovic Courtès <[email-masked]>
Date: Sun, 12 Feb 2023 00:55:58 +0100

Hi Reily,

Reily Siegel <[email-masked]> skribis:

> * guix/lint.scm (file-module): New function.
> (module-dependencies): New function.
> (sexp-symbols): New function.
> (file-symbols): New function.
> (report-unused-module): New function.
> (check-unused-modules): New function.
> (%local-checkers): Add check-unused-modules.

Nice!  This is a hot topic, discussed at the Guix Days last week!

  https://lists.gnu.org/archive/html/guix-devel/2023-02/msg00028.html

[...]

> +(define (check-unused-modules package)
> +  "Check if the file in which PACKAGE is defined contains unused module imports."
> +  (let* ((file (package-file package))
> +         (symbols (file-symbols file))
> +         (dependencies (module-dependencies (file-module file))))
> +    (fold
> +     (match-lambda*
> +       (((module . publics) res)
> +        (if (null? (lset-intersection eq? publics symbols))
> +            (cons (report-unused-module package module)
> +                  res)
> +            res)))
> +     '()
> +     dependencies)))

As you may know, this is an approximation: it doesn’t take into account
shadowed bindings, module import renamers and selections, and so on; it
might think a module is used just because a symbol with the same name as
one it exports appears somewhere in the code.  In practice, it probably
works quite well for package modules though, right?

I have just submitted a patch adding a ‘-Wunused-module’ warning to
Guile’s compiler, which avoids these issues:

  https://lists.gnu.org/archive/html/guile-devel/2023-02/msg00026.html

I wonder if we should stick to that and avoid having a lint warning
altogether.  WDYT?

Thanks,
Ludo’.