From 1480865e641b06d570f5ab56011f8e3e5481da7d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Wed, 28 Dec 2022 14:40:19 +0100 Subject: [PATCH] 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. --- lisp/subr.el | 13 +++++++++++-- test/lisp/emacs-lisp/bytecomp-tests.el | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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) -- 2.39.2