From patchwork Sat Feb 4 05:59:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Reily Siegel X-Patchwork-Id: 46665 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 1882527BBEB; Sat, 4 Feb 2023 06:46:32 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 222B327BBE9 for ; Sat, 4 Feb 2023 06:46:28 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pOCJR-0001pE-A1; Sat, 04 Feb 2023 01:46:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pOCJH-0001ok-Ko for guix-patches@gnu.org; Sat, 04 Feb 2023 01:46:08 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pOCJG-000055-Fe for guix-patches@gnu.org; Sat, 04 Feb 2023 01:46:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pOCJG-00025x-9k for guix-patches@gnu.org; Sat, 04 Feb 2023 01:46:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#61266] [PATCH 1/1] lint: Add unused-modules linter. Resent-From: Reily Siegel Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 04 Feb 2023 06:46:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61266 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 61266@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16754931437876 (code B ref -1); Sat, 04 Feb 2023 06:46:02 +0000 Received: (at submit) by debbugs.gnu.org; 4 Feb 2023 06:45:43 +0000 Received: from localhost ([127.0.0.1]:40505 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOCIu-00022v-75 for submit@debbugs.gnu.org; Sat, 04 Feb 2023 01:45:43 -0500 Received: from lists.gnu.org ([209.51.188.17]:39434) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pOCIs-00022n-Ks for submit@debbugs.gnu.org; Sat, 04 Feb 2023 01:45:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pOCIr-0001lT-KS for guix-patches@gnu.org; Sat, 04 Feb 2023 01:45:37 -0500 Received: from mout-p-103.mailbox.org ([80.241.56.161]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1pOCIp-0008V6-B2 for guix-patches@gnu.org; Sat, 04 Feb 2023 01:45:37 -0500 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4P831l1mlYz9scc for ; Sat, 4 Feb 2023 07:45:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=reilysiegel.com; s=MBO0001; t=1675493131; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+zxGffCQ1EjRI7VC0CR+EaiCnUejpi84nXrqASlg1mw=; b=pwpu2jzjgdivKHuidVrfw57KEUKAgeU1GQpVz6wexW0iKWrwHtJ1fk2AYespKKZscyjKrJ fcbtVYtDeWQS6A/h42jv7st72shk1Uv+TimYMeTZyE9Hk/NdmvzY1xBoiFQRdFtbp+5qQF z9cevoigau1JZhUl0B1BbfRYCS9i4PvAYHHbbwkyCqoFVEnRRrOP0zokDXTp711qUsQn35 NsY42DVHiq+rIDuOtj2k/5ehOA7km4ynx6G+YgNfSjvcUgPt4hjn7zSPB3BSI+6v7oEBRi Aqp08gyluUe5+63iPmwj69+Slc/TKUe+/mms8gATFKk7XEi/drSuev8PeY97Pw== From: Reily Siegel Date: Sat, 4 Feb 2023 00:59:42 -0500 Message-ID: <87h6w2gc8n.fsf@reilysiegel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=80.241.56.161; envelope-from=mail@reilysiegel.com; helo=mout-p-103.mailbox.org X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * 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 ;;; Copyright © 2021, 2022 Maxime Devos ;;; Copyright © 2021 Brice Waegeneire +;;; Copyright © 2023 Reily Siegel ;;; ;;; 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