Subject: Re: bug#16567: 24.3.50; pcase should signal an error if no case matches

From: Leo Liu <[email-masked]>
Date: Tue, 09 Sep 2014 02:27:08 +0800

On 2014-01-27 10:22 +0100, Helmut Eller wrote:
> pcase returns nil for this example:
>
>  (pcase 3
>    (1 1)
>    (2 2))
>
> it would be more useful if it would signal an error instead.  Manually
> adding a "catch-all case" and inventing some error message is tedious
> and it's also easy to forget.

Hi Stefan,

I also find myself wanting something as Helmut suggested more and more.
Any comments in the following patch? Thanks. Leo

=== modified file 'lisp/emacs-lisp/pcase.el'
--- lisp/emacs-lisp/pcase.el	2014-09-06 00:59:00 +0000
+++ lisp/emacs-lisp/pcase.el	2014-09-08 18:15:37 +0000
@@ -68,6 +68,8 @@
 
 (defconst pcase--dontcare-upats '(t _ pcase--dontcare))
 
+(defvar pcase--dontwarn-upats '(pcase--dontcare))
+
 (def-edebug-spec
   pcase-UPAT
   (&or symbolp
@@ -148,6 +150,14 @@
         ;; (puthash (car cases) `(,exp ,cases ,@expansion) pcase--memoize-2)
         expansion))))
 
+;;;###autoload
+(defmacro xcase (exp &rest cases)
+  (declare (indent 1) (debug pcase))
+  (let* ((x (make-symbol "x"))
+         (pcase--dontwarn-upats (cons x pcase--dontwarn-upats)))
+    (pcase--expand
+     exp (append cases `((,x (error "No clause matching `%S'" ,x)))))))
+
 (defun pcase--let* (bindings body)
   (cond
    ((null bindings) (macroexp-progn body))
@@ -280,7 +290,8 @@
                              vars))))
                      cases))))
       (dolist (case cases)
-        (unless (or (memq case used-cases) (eq (car case) 'pcase--dontcare))
+        (unless (or (memq case used-cases)
+                    (memq (car case) pcase--dontwarn-upats))
           (message "Redundant pcase pattern: %S" (car case))))
       (macroexp-let* defs main))))