From: Mattias EngdegÄrd Date: Wed, 28 Dec 2022 13:40:19 +0000 (+0100) Subject: Warn about `ignore-error` with quoted condition argument X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1480865e641b06d570f5ab56011f8e3e5481da7d;p=emacs.git Warn about `ignore-error` with quoted condition argument * lisp/subr.el (ignore-error): Clarify condition argument in doc string and add warning. * test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-warn-quoted-condition): New test. --- diff --git a/lisp/subr.el b/lisp/subr.el index f0081de0619..5e8f3c82a2a 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -380,9 +380,18 @@ without silencing all errors." "Execute BODY; if the error CONDITION occurs, return nil. Otherwise, return result of last form in BODY. -CONDITION can also be a list of error conditions." +CONDITION can also be a list of error conditions. +The CONDITION argument is not evaluated. Do not quote it." (declare (debug t) (indent 1)) - `(condition-case nil (progn ,@body) (,condition nil))) + (if (and (eq (car-safe condition) 'quote) + (cdr condition) (null (cddr condition))) + (macroexp-warn-and-return + (format "`ignore-error' condition argument should not be quoted: %S" + condition) + `(condition-case nil (progn ,@body) (,(cadr condition) nil)) + nil t condition) + `(condition-case nil (progn ,@body) (,condition nil)))) + ;;;; Basic Lisp functions. diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el index 47200de7a02..0d62283c04a 100644 --- a/test/lisp/emacs-lisp/bytecomp-tests.el +++ b/test/lisp/emacs-lisp/bytecomp-tests.el @@ -922,6 +922,11 @@ byte-compiled. Run with dynamic binding." (bytecomp--with-warning-test "defvar.*foo.*wider than.*characters" `(defvar foo t ,bytecomp-tests--docstring))) +(ert-deftest bytecomp-warn-quoted-condition () + (bytecomp--with-warning-test + "Warning: `ignore-error' condition argument should not be quoted: 'error" + '(ignore-error 'error (abc)))) + (ert-deftest bytecomp-warn-dodgy-args-eq () (dolist (fn '(eq eql)) (cl-flet ((msg (type arg)