From: Mattias EngdegÄrd Date: Wed, 6 Nov 2024 12:29:23 +0000 (+0100) Subject: Fix wrong value of `when` and `unless` with empty body (bug#74215) X-Git-Tag: archive/raspbian/1%30.1+1-3+rpi1^2~2^2~20^2~291 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=e88309eef30abca327c845fe4133d758d33cfb6b;p=emacs.git Fix wrong value of `when` and `unless` with empty body (bug#74215) * lisp/subr.el (when, unless): Return nil when the body is empty. Reported by Brennan Vincent. * test/lisp/subr-tests.el (subr-test-when): Add test cases. (cherry picked from commit 9ee9154247454c18f9f75d0d32592b817d7e977a) --- diff --git a/lisp/subr.el b/lisp/subr.el index b849d8d1b16..465795c7555 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'."