[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 1480865e64 3/4: Warn about `ignore-error` with quoted condition a
From: |
Mattias Engdegård |
Subject: |
master 1480865e64 3/4: Warn about `ignore-error` with quoted condition argument |
Date: |
Thu, 29 Dec 2022 06:44:03 -0500 (EST) |
branch: master
commit 1480865e641b06d570f5ab56011f8e3e5481da7d
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>
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 f0081de061..5e8f3c82a2 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 47200de7a0..0d62283c04 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)