]> git.eshelyaron.com Git - emacs.git/commitdiff
Warn about `condition-case` without handlers
authorMattias Engdegård <mattiase@acm.org>
Mon, 27 Feb 2023 12:57:48 +0000 (13:57 +0100)
committerMattias Engdegård <mattiase@acm.org>
Mon, 27 Feb 2023 13:34:22 +0000 (14:34 +0100)
Omitting handlers from a `condition-case` form makes it useless
since no errors are caught.

* lisp/emacs-lisp/macroexp.el (macroexp--expand-all): New warning.
* test/lisp/emacs-lisp/bytecomp-tests.el
(bytecomp-test--with-suppressed-warnings): Add test case.
* etc/NEWS: Announce.

etc/NEWS
lisp/emacs-lisp/macroexp.el
test/lisp/emacs-lisp/bytecomp-tests.el

index 4b0e4e6bd461a9e9c052d4384d5013f314042011..9241598f185940d4b2af7fb1e76798811908be2c 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -321,6 +321,21 @@ compared reliably at all.
 This warning can be suppressed using 'with-suppressed-warnings' with
 the warning name 'suspicious'.
 
+---
+*** Warn about 'condition-case' without handlers.
+The compiler now warns when the 'condition-case' form is used without
+any actual handlers, as in
+
+    (condition-case nil (read buffer))
+
+because it has no effect other than the execution of the body form.
+In particular, no errors are caught or suppressed.  If the intention
+was to catch all errors, add an explicit handler for 'error', or use
+'ignore-error' or 'ignore-errors'.
+
+This warning can be suppressed using 'with-suppressed-warnings' with
+the warning name 'suspicious'.
+
 +++
 ** New function 'file-user-uid'.
 This function is like 'user-uid', but is aware of file name handlers,
index c57a27069d6f6764ed44b8ce4efcf8b1db456a8c..8cb67c3b8b5a368b77422589977c5e01304deb9a 100644 (file)
@@ -339,14 +339,19 @@ Assumes the caller has bound `macroexpand-all-environment'."
             (`(cond . ,clauses)
              (macroexp--cons fn (macroexp--all-clauses clauses) form))
             (`(condition-case . ,(or `(,err ,body . ,handlers) pcase--dontcare))
-             (macroexp--cons
-              fn
-              (macroexp--cons err
-                              (macroexp--cons (macroexp--expand-all body)
-                                              (macroexp--all-clauses handlers 1)
-                                              (cddr form))
-                              (cdr form))
-              form))
+             (let ((exp-body (macroexp--expand-all body)))
+               (if handlers
+                   (macroexp--cons fn
+                                   (macroexp--cons
+                                    err (macroexp--cons
+                                         exp-body
+                                         (macroexp--all-clauses handlers 1)
+                                         (cddr form))
+                                    (cdr form))
+                                   form)
+                 (macroexp-warn-and-return
+                  (format-message "`condition-case' without handlers")
+                  exp-body (list 'suspicious 'condition-case) t form))))
             (`(,(or 'defvar 'defconst) ,(and name (pred symbolp)) . ,_)
              (push name macroexp--dynvars)
              (macroexp--all-forms form 2))
index 185abaf5c22d2c8c5f13eea40593cec5e45c6fba..b6dcfeedb0c1cf1f76557bb4e5e70d8a514d2899 100644 (file)
@@ -1446,6 +1446,12 @@ literals (Bug#20852)."
    '((suspicious set-buffer))
    "Warning: Use .with-current-buffer. rather than")
 
+  (test-suppression
+   '(defun zot (x)
+      (condition-case nil (list x)))
+   '((suspicious condition-case))
+   "Warning: `condition-case' without handlers")
+
   (test-suppression
    '(defun zot ()
       (let ((_ 1))