Subject: Re: bug#69108: false-positive warning "variable ‘_’ not left unused" in if-let* and if-let

From: Michael Heerdegen <[email-masked]>
Date: Sat, 17 Feb 2024 01:28:55 +0100

I <[email-masked]> write:

> Would be a one liner to make both cases generate the same expansion.

From 906355a716864c87aa0ea112ac890ec9f59d0089 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <[email-masked]>
Date: Fri, 16 Feb 2024 22:07:18 +0100
Subject: [PATCH] Don't warn about _ not left unused in if-let and alike

Fix Bug#69108: The macro expansions did not leave a variable _ unused;
this triggered a compiler warning.

* lisp/subr.el (internal--build-binding): Handle (_ FORM) separately.
(if-let, and-let*): Tweak doc.
---
 lisp/subr.el | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index c317d558e24..4f22f0c6b3f 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2575,12 +2575,12 @@ delay-mode-hooks
 (defun internal--build-binding (binding prev-var)
   "Check and build a single BINDING with PREV-VAR."
   (setq binding
-        (cond
-         ((symbolp binding)
+        (pcase binding
+         ((pred symbolp)
           (list binding binding))
-         ((null (cdr binding))
-          (list (make-symbol "s") (car binding)))
-         (t binding)))
+         ((or `(,test) `(_ ,test))
+          (list (make-symbol "s") test))
+         (_ binding)))
   (when (> (length binding) 2)
     (signal 'error
             (cons "`let' bindings can have only one value-form" binding)))
@@ -2620,7 +2620,7 @@ when-let*
 (defmacro and-let* (varlist &rest body)
   "Bind variables according to VARLIST and conditionally evaluate BODY.
 Like `when-let*', except if BODY is empty and all the bindings
-are non-nil, then the result is non-nil."
+are non-nil, then the result is the value of the last binding."
   (declare (indent 1) (debug if-let*))
   (let (res)
     (if varlist
@@ -2631,9 +2631,9 @@ and-let*

 (defmacro if-let (spec then &rest else)
   "Bind variables according to SPEC and evaluate THEN or ELSE.
-Evaluate each binding in turn, as in `let*', stopping if a
-binding value is nil.  If all are non-nil return the value of
-THEN, otherwise the last form in ELSE.
+Evaluate each binding in turn, as in `let*', stopping if a binding value
+is nil.  If all are non-nil return the value of THEN, otherwise the
+value of the last ELSE form or nil if there are none.

 Each element of SPEC is a list (SYMBOL VALUEFORM) that binds
 SYMBOL to the value of VALUEFORM.  An element can additionally be
@@ -2642,9 +2642,9 @@ if-let
 interest.  It can also be of the form SYMBOL, then the binding of
 SYMBOL is checked for nil.

-As a special case, interprets a SPEC of the form \(SYMBOL SOMETHING)
-like \((SYMBOL SOMETHING)).  This exists for backward compatibility
-with an old syntax that accepted only one binding."
+As a special case that exists for backward compatibility only, a
+complete SPEC of the form \(SYMBOL SOMETHING) is interpreted like
+\((SYMBOL SOMETHING))."
   (declare (indent 2)
            (debug ([&or (symbolp form)  ; must be first, Bug#48489
                         (&rest [&or symbolp (symbolp form) (form)])]
--
2.39.2

Michael.