From 08291e6f1755837d1cc0d5ac940bd39f7bcadd43 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Mon, 29 May 2023 17:49:48 +0200 Subject: [PATCH] Clean up defcustom type quote check * lisp/emacs-lisp/bytecomp.el (byte-compile--suspicious-defcustom-choice): Rename to... (byte-compile--defcustom-type-quoted): ...this and rewrite to make more sense. All callers updated. (byte-compile-nogroup-warn): Better warning message. * test/lisp/emacs-lisp/bytecomp-tests.el (test-bytecomp-defgroup-choice): This never failed because it wasn't actually a test. Turn it into... (bytecomp-test-defcustom-type-quoted): ...this, which is. --- lisp/emacs-lisp/bytecomp.el | 29 +++++++++++++------------- test/lisp/emacs-lisp/bytecomp-tests.el | 8 +++---- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index ac040799a22..aea50fc8e57 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -1619,21 +1619,20 @@ extra args." (dolist (elt '(format message format-message error)) (put elt 'byte-compile-format-like t)) -(defun byte-compile--suspicious-defcustom-choice (type) - "Say whether defcustom TYPE looks odd." - ;; Check whether there's anything like (choice (const :tag "foo" ;; 'bar)). +(defun byte-compile--defcustom-type-quoted (type) + "Whether defcustom TYPE contains an accidentally quoted value." + ;; Detect mistakes such as (const 'abc). ;; We don't actually follow the syntax for defcustom types, but this ;; should be good enough. - (catch 'found - (if (and (consp type) - (proper-list-p type)) - (if (memq (car type) '(const other)) - (when (assq 'quote type) - (throw 'found t)) - (when (memq t (mapcar #'byte-compile--suspicious-defcustom-choice - type)) - (throw 'found t))) - nil))) + (and (consp type) + (proper-list-p type) + (if (memq (car type) '(const other)) + (assq 'quote type) + (let ((elts (cdr type))) + (while (and elts (not (byte-compile--defcustom-type-quoted + (car elts)))) + (setq elts (cdr elts))) + elts)))) ;; Warn if a custom definition fails to specify :group, or :type. (defun byte-compile-nogroup-warn (form) @@ -1647,10 +1646,10 @@ extra args." (byte-compile-warn-x (cadr name) "defcustom for `%s' fails to specify type" (cadr name))) - ((byte-compile--suspicious-defcustom-choice type) + ((byte-compile--defcustom-type-quoted type) (byte-compile-warn-x (cadr name) - "defcustom for `%s' has syntactically odd type `%s'" + "defcustom for `%s' may have accidentally quoted value in type `%s'" (cadr name) type))))) (if (and (memq (car form) '(custom-declare-face custom-declare-variable)) byte-compile-current-group) diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el index a8809bda81c..963ea9abe0c 100644 --- a/test/lisp/emacs-lisp/bytecomp-tests.el +++ b/test/lisp/emacs-lisp/bytecomp-tests.el @@ -1799,11 +1799,11 @@ EXPECTED-POINT BINDINGS (MODES \\='\\='(ruby-mode js-mode python-mode)) \ (TEST-IN-COMMENTS t) (TEST-IN-STRINGS t) (TEST-IN-CODE t) \ (FIXTURE-FN \\='#\\='electric-pair-mode))" fill-column))) -(defun test-bytecomp-defgroup-choice () - (should-not (byte-compile--suspicious-defcustom-choice 'integer)) - (should-not (byte-compile--suspicious-defcustom-choice +(ert-deftest bytecomp-test-defcustom-type-quoted () + (should-not (byte-compile--defcustom-type-quoted 'integer)) + (should-not (byte-compile--defcustom-type-quoted '(choice (const :tag "foo" bar)))) - (should (byte-compile--suspicious-defcustom-choice + (should (byte-compile--defcustom-type-quoted '(choice (const :tag "foo" 'bar))))) (ert-deftest bytecomp-function-attributes () -- 2.39.2