diff options
| author | Lars Ingebrigtsen | 2022-05-11 12:51:11 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-05-11 12:51:11 +0200 |
| commit | 231cf5ee2bed8a2b574ad424b624b36c0ee0733f (patch) | |
| tree | 33fc90e66dbec5f0a990b14d27c35c6bb9946a93 | |
| parent | 5a121485037a21d03449bc955476c253c89e3671 (diff) | |
| download | emacs-231cf5ee2bed8a2b574ad424b624b36c0ee0733f.tar.gz emacs-231cf5ee2bed8a2b574ad424b624b36c0ee0733f.zip | |
Warn about quoted symbols in defcustom choice/other forms
* lisp/emacs-lisp/bytecomp.el
(byte-compile--suspicious-defcustom-choice): New function (bug#16271).
(byte-compile-nogroup-warn): Use it to warn about forms like
(choice (const :tag "foo" 'bar)).
| -rw-r--r-- | etc/NEWS | 13 | ||||
| -rw-r--r-- | lisp/emacs-lisp/bytecomp.el | 32 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/bytecomp-tests.el | 6 |
3 files changed, 47 insertions, 4 deletions
| @@ -1767,6 +1767,19 @@ functions. | |||
| 1767 | 1767 | ||
| 1768 | * Lisp Changes in Emacs 29.1 | 1768 | * Lisp Changes in Emacs 29.1 |
| 1769 | 1769 | ||
| 1770 | ** Byte compilation | ||
| 1771 | |||
| 1772 | --- | ||
| 1773 | *** Byte compilation will now warn about some malformed 'defcustom' types. | ||
| 1774 | It's very common to write 'defcustom' types on the form: | ||
| 1775 | |||
| 1776 | :type '(choice (const :tag "foo" 'bar)) | ||
| 1777 | |||
| 1778 | I.e., double-quoting the 'bar', which is almost never the correct | ||
| 1779 | value. The byte compiler will now issue a warning if it encounters | ||
| 1780 | these forms. | ||
| 1781 | |||
| 1782 | |||
| 1770 | +++ | 1783 | +++ |
| 1771 | *** 'restore-buffer-modified-p' can now alter buffer auto-save state. | 1784 | *** 'restore-buffer-modified-p' can now alter buffer auto-save state. |
| 1772 | With a FLAG value of 'autosaved', it will mark the buffer as having | 1785 | With a FLAG value of 'autosaved', it will mark the buffer as having |
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index c0dffe544cf..cbf2659109a 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el | |||
| @@ -1562,15 +1562,39 @@ extra args." | |||
| 1562 | (dolist (elt '(format message error)) | 1562 | (dolist (elt '(format message error)) |
| 1563 | (put elt 'byte-compile-format-like t)) | 1563 | (put elt 'byte-compile-format-like t)) |
| 1564 | 1564 | ||
| 1565 | (defun byte-compile--suspicious-defcustom-choice (type) | ||
| 1566 | "Say whether defcustom TYPE looks odd." | ||
| 1567 | ;; Check whether there's anything like (choice (const :tag "foo" ;; 'bar)). | ||
| 1568 | ;; We don't actually follow the syntax for defcustom types, but this | ||
| 1569 | ;; should be good enough. | ||
| 1570 | (catch 'found | ||
| 1571 | (if (and (consp type) | ||
| 1572 | (proper-list-p type)) | ||
| 1573 | (if (memq (car type) '(const other)) | ||
| 1574 | (when (assq 'quote type) | ||
| 1575 | (throw 'found t)) | ||
| 1576 | (when (memq t (mapcar #'byte-compile--suspicious-defcustom-choice | ||
| 1577 | type)) | ||
| 1578 | (throw 'found t))) | ||
| 1579 | nil))) | ||
| 1580 | |||
| 1565 | ;; Warn if a custom definition fails to specify :group, or :type. | 1581 | ;; Warn if a custom definition fails to specify :group, or :type. |
| 1566 | (defun byte-compile-nogroup-warn (form) | 1582 | (defun byte-compile-nogroup-warn (form) |
| 1567 | (let ((keyword-args (cdr (cdr (cdr (cdr form))))) | 1583 | (let ((keyword-args (cdr (cdr (cdr (cdr form))))) |
| 1568 | (name (cadr form))) | 1584 | (name (cadr form))) |
| 1569 | (when (eq (car-safe name) 'quote) | 1585 | (when (eq (car-safe name) 'quote) |
| 1570 | (or (not (eq (car form) 'custom-declare-variable)) | 1586 | (when (eq (car form) 'custom-declare-variable) |
| 1571 | (plist-get keyword-args :type) | 1587 | (let ((type (plist-get keyword-args :type))) |
| 1572 | (byte-compile-warn-x (cadr name) | 1588 | (cond |
| 1573 | "defcustom for `%s' fails to specify type" (cadr name))) | 1589 | ((not type) |
| 1590 | (byte-compile-warn-x (cadr name) | ||
| 1591 | "defcustom for `%s' fails to specify type" | ||
| 1592 | (cadr name))) | ||
| 1593 | ((byte-compile--suspicious-defcustom-choice type) | ||
| 1594 | (byte-compile-warn-x | ||
| 1595 | (cadr name) | ||
| 1596 | "defcustom for `%s' has syntactically odd type `%s'" | ||
| 1597 | (cadr name) type))))) | ||
| 1574 | (if (and (memq (car form) '(custom-declare-face custom-declare-variable)) | 1598 | (if (and (memq (car form) '(custom-declare-face custom-declare-variable)) |
| 1575 | byte-compile-current-group) | 1599 | byte-compile-current-group) |
| 1576 | ;; The group will be provided implicitly. | 1600 | ;; The group will be provided implicitly. |
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el index abd33ab8e5a..051e8b9e5c9 100644 --- a/test/lisp/emacs-lisp/bytecomp-tests.el +++ b/test/lisp/emacs-lisp/bytecomp-tests.el | |||
| @@ -1538,6 +1538,12 @@ EXPECTED-POINT BINDINGS (MODES \\='\\='(ruby-mode js-mode python-mode)) \ | |||
| 1538 | (TEST-IN-COMMENTS t) (TEST-IN-STRINGS t) (TEST-IN-CODE t) \ | 1538 | (TEST-IN-COMMENTS t) (TEST-IN-STRINGS t) (TEST-IN-CODE t) \ |
| 1539 | (FIXTURE-FN \\='#\\='electric-pair-mode))" fill-column))) | 1539 | (FIXTURE-FN \\='#\\='electric-pair-mode))" fill-column))) |
| 1540 | 1540 | ||
| 1541 | (defun test-bytecomp-defgroup-choice () | ||
| 1542 | (should-not (byte-compile--suspicious-defcustom-choice 'integer)) | ||
| 1543 | (should-not (byte-compile--suspicious-defcustom-choice | ||
| 1544 | '(choice (const :tag "foo" bar)))) | ||
| 1545 | (should (byte-compile--suspicious-defcustom-choice | ||
| 1546 | '(choice (const :tag "foo" 'bar))))) | ||
| 1541 | 1547 | ||
| 1542 | ;; Local Variables: | 1548 | ;; Local Variables: |
| 1543 | ;; no-byte-compile: t | 1549 | ;; no-byte-compile: t |