]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix wrong value of `when` and `unless` with empty body (bug#74215)
authorMattias EngdegÄrd <mattiase@acm.org>
Wed, 6 Nov 2024 12:29:23 +0000 (13:29 +0100)
committerEshel Yaron <me@eshelyaron.com>
Fri, 8 Nov 2024 13:34:01 +0000 (14:34 +0100)
Reported by Brennan Vincent.

* lisp/subr.el (when, unless): Return nil when the body is empty.
* test/lisp/subr-tests.el (subr-test-when): Add test cases.

(cherry picked from commit 9ee9154247454c18f9f75d0d32592b817d7e977a)

lisp/subr.el
test/lisp/subr-tests.el

index 40394f1d7e3436d3204c5ede072b34a05fbb0059..e6f947b4eef1c21063bd92805d567dbc234a3c11 100644 (file)
@@ -299,7 +299,7 @@ value of last one, or nil if there are none."
   (if body
       (list 'if cond (cons 'progn body))
     (macroexp-warn-and-return (format-message "`when' with empty body")
-                              cond '(empty-body when) t)))
+                              (list 'progn cond nil) '(empty-body when) t)))
 
 (defmacro unless (cond &rest body)
   "If COND yields nil, do BODY, else return nil.
@@ -309,7 +309,7 @@ value of last one, or nil if there are none."
   (if body
       (cons 'if (cons cond (cons nil body)))
     (macroexp-warn-and-return (format-message "`unless' with empty body")
-                              cond '(empty-body unless) t)))
+                              (list 'progn cond nil) '(empty-body unless) t)))
 
 (defsubst subr-primitive-p (object)
   "Return t if OBJECT is a built-in primitive written in C.
index 6f28e0573424708604e4ab5adee7d037939f2558..e12e3c62e0ce67961afd5dbf78b4377adf253182 100644 (file)
                    x)))
     (should (= x 2)))
   (should (equal (macroexpand-all '(when a b c d))
-                 '(if a (progn b c d)))))
+                 '(if a (progn b c d))))
+  (with-suppressed-warnings ((empty-body when unless))
+    (should (equal (when t) nil))
+    (should (equal (unless t) nil))
+    (should (equal (unless nil) nil))))
 
 (ert-deftest subr-test-xor ()
   "Test `xor'."