Subject: [PATCH 1/1] lint: Add unused-modules linter.
From: Reily Siegel <[email-masked]>
To: [email-masked]
Date: Sat, 4 Feb 2023 00:59:42 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

* 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.
---
 guix/lint.scm | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/guix/lint.scm b/guix/lint.scm
index 8e3976171f..38c8595bd6 100644
--- a/guix/lint.scm
+++ b/guix/lint.scm
@@ -14,6 +14,7 @@
 ;;; Copyright © 2021 Xinglu Chen <[email-masked]>
 ;;; Copyright © 2021, 2022 Maxime Devos <[email-masked]>
 ;;; Copyright © 2021 Brice Waegeneire <[email-masked]>
+;;; Copyright © 2023 Reily Siegel <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -37,6 +38,7 @@ (define-module (guix lint)
   #:autoload   (guix base64) (base64-encode)
   #:use-module (guix build-system)
   #:use-module (guix diagnostics)
+  #:use-module (guix discovery)
   #:use-module (guix download)
   #:use-module (guix ftp-client)
   #:use-module (guix http-client)
@@ -1843,6 +1845,62 @@ (define (check-formatting package)
                                        (G_ "source file not found"))))))))
         '())))
 
+(define (file-module file)
+  "Return the resolved module defined in FILE."
+  (call-with-input-file file
+    (lambda (port)
+      (let loop ()
+        (match (read port)
+          (('define-module module . _) (resolve-module module))
+          ((? eof-object?) #f)
+          (_ (loop)))))))
+
+(define (module-dependencies module)
+  "Return an alist of (module . public-exports) for each of MODULE's imports."
+  (fold-module-public-variables*
+   (lambda (module sym _ alist)
+     (assoc-set! alist module (cons sym (or (assoc-ref alist module) '()))))
+   '()
+   (module-uses module)))
+
+(define (sexp-symbols sexp)
+  "Return all symols in SEXP."
+  (match sexp
+    ((? symbol?) (list sexp))
+    ((? list?) (apply append (map sexp-symbols sexp)))
+    (_ '())))
+
+(define (file-symbols file)
+  "Return all symbols in FILE."
+  (call-with-input-file file
+    (lambda (port)
+      (let loop ((res '()))
+        (let ((sexp (read port)))
+          (if (eof-object? sexp)
+              res
+              (loop (append res (sexp-symbols sexp)))))))))
+
+(define (report-unused-module package module)
+  "Report a warning that MODULE is not used in the file where PACKAGE is defined."
+  (make-warning package
+                (G_ "Imported module ~a is not used.")
+                (list (module-name module))))
+
+(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)))
+
 
 ;;;
 ;;; List of checkers.
@@ -1922,7 +1980,11 @@ (define %local-checkers
    (lint-checker
      (name        'formatting)
      (description "Look for formatting issues in the source")
-     (check       check-formatting))))
+     (check       check-formatting))
+   (lint-checker
+     (name        'unused-modules)
+     (description "Look for unused modules in the package file")
+     (check       check-unused-modules))))
 
 (define %network-dependent-checkers
   (list