emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 6d8f5161ea 1/2: Signal an error if a fallback cl-case is misplace


From: Lars Ingebrigtsen
Subject: master 6d8f5161ea 1/2: Signal an error if a fallback cl-case is misplaced
Date: Tue, 13 Sep 2022 11:20:19 -0400 (EDT)

branch: master
commit 6d8f5161ead689b7a2e44a7de0a695f0ab4c833b
Author: Philipp Stephani <phst@google.com>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Signal an error if a fallback cl-case is misplaced
    
    * lisp/emacs-lisp/cl-macs.el (cl-case): Warn if the user passes a nil
    key list (which would never match).  Warn about quoted symbols that
    should probably be unquoted.
    
    * test/lisp/emacs-lisp/cl-macs-tests.el (cl-case-warning): New unit
    test (bug#51368).
---
 lisp/emacs-lisp/cl-macs.el            |  9 +++++++--
 test/lisp/emacs-lisp/cl-macs-tests.el | 11 +++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el
index f8fdc50251..946d2c09a9 100644
--- a/lisp/emacs-lisp/cl-macs.el
+++ b/lisp/emacs-lisp/cl-macs.el
@@ -775,11 +775,16 @@ compared by `eql'.
 \(fn EXPR (KEYLIST BODY...)...)"
   (declare (indent 1) (debug (form &rest (sexp body))))
   (macroexp-let2 macroexp-copyable-p temp expr
-    (let* ((head-list nil))
+    (let* ((head-list nil)
+           (has-otherwise nil))
       `(cond
         ,@(mapcar
            (lambda (c)
-             (cons (cond ((memq (car c) '(t otherwise)) t)
+             (cons (cond (has-otherwise
+                          (error "Misplaced t or `otherwise' clause"))
+                         ((memq (car c) '(t otherwise))
+                          (setq has-otherwise t)
+                          t)
                          ((eq (car c) 'cl--ecase-error-flag)
                           `(error "cl-ecase failed: %s, %s"
                                   ,temp ',(reverse head-list)))
diff --git a/test/lisp/emacs-lisp/cl-macs-tests.el 
b/test/lisp/emacs-lisp/cl-macs-tests.el
index 68898720d9..77817abd85 100644
--- a/test/lisp/emacs-lisp/cl-macs-tests.el
+++ b/test/lisp/emacs-lisp/cl-macs-tests.el
@@ -747,4 +747,15 @@ collection clause."
       ;; Just make sure the forms can be instrumented.
       (eval-buffer))))
 
+(ert-deftest cl-case-error ()
+  "Test that `cl-case' and `cl-ecase' signal an error if a t or
+`otherwise' key is misplaced."
+  (dolist (form '((cl-case val (t 1) (123 2))
+                  (cl-ecase val (t 1) (123 2))
+                  (cl-ecase val (123 2) (t 1))))
+    (ert-info ((prin1-to-string form) :prefix "Form: ")
+      (let ((error (should-error (macroexpand form))))
+        (should (equal (cdr error)
+                       '("Misplaced t or `otherwise' clause")))))))
+
 ;;; cl-macs-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]