From 0ea4f126ebc6579ac62613cb040107de47eff6d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Wed, 6 Nov 2024 13:29:23 +0100 Subject: [PATCH] Fix wrong value of `when` and `unless` with empty body (bug#74215) 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 | 4 ++-- test/lisp/subr-tests.el | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index 40394f1d7e3..e6f947b4eef 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -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. diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 6f28e057342..e12e3c62e0c 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -454,7 +454,11 @@ 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'." -- 2.39.5