aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Stephani2022-09-13 17:12:57 +0200
committerLars Ingebrigtsen2022-09-13 17:12:57 +0200
commitfffa53ff1afe097fe38f7664df5debe9811201d1 (patch)
treecdc34b558e9b4684b8362706a75a5a2d563f1ec8
parent6d8f5161ead689b7a2e44a7de0a695f0ab4c833b (diff)
downloademacs-fffa53ff1afe097fe38f7664df5debe9811201d1.tar.gz
emacs-fffa53ff1afe097fe38f7664df5debe9811201d1.zip
Have 'cl-case' warn about suspicious cases
* 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).
-rw-r--r--lisp/emacs-lisp/cl-macs.el15
-rw-r--r--test/lisp/emacs-lisp/cl-macs-tests.el32
2 files changed, 47 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el
index 946d2c09a92..5d330f32d66 100644
--- a/lisp/emacs-lisp/cl-macs.el
+++ b/lisp/emacs-lisp/cl-macs.el
@@ -788,6 +788,21 @@ compared by `eql'.
788 ((eq (car c) 'cl--ecase-error-flag) 788 ((eq (car c) 'cl--ecase-error-flag)
789 `(error "cl-ecase failed: %s, %s" 789 `(error "cl-ecase failed: %s, %s"
790 ,temp ',(reverse head-list))) 790 ,temp ',(reverse head-list)))
791 ((null (car c))
792 (macroexp-warn-and-return
793 "Case nil will never match"
794 nil 'suspicious))
795 ((and (consp (car c)) (not (cddar c))
796 (memq (caar c) '(quote function)))
797 (macroexp-warn-and-return
798 (format-message
799 (concat "Case %s will match `%s'. If "
800 "that's intended, write %s "
801 "instead. Otherwise, don't "
802 "quote `%s'.")
803 (car c) (caar c) (list (cadar c) (caar c))
804 (cadar c))
805 `(cl-member ,temp ',(car c)) 'suspicious))
791 ((listp (car c)) 806 ((listp (car c))
792 (setq head-list (append (car c) head-list)) 807 (setq head-list (append (car c) head-list))
793 `(cl-member ,temp ',(car c))) 808 `(cl-member ,temp ',(car c)))
diff --git a/test/lisp/emacs-lisp/cl-macs-tests.el b/test/lisp/emacs-lisp/cl-macs-tests.el
index 77817abd85c..427b8f46893 100644
--- a/test/lisp/emacs-lisp/cl-macs-tests.el
+++ b/test/lisp/emacs-lisp/cl-macs-tests.el
@@ -25,6 +25,8 @@
25(require 'cl-macs) 25(require 'cl-macs)
26(require 'edebug) 26(require 'edebug)
27(require 'ert) 27(require 'ert)
28(require 'ert-x)
29(require 'pcase)
28 30
29 31
30;;;; cl-loop tests -- many adapted from Steele's CLtL2 32;;;; cl-loop tests -- many adapted from Steele's CLtL2
@@ -758,4 +760,34 @@ collection clause."
758 (should (equal (cdr error) 760 (should (equal (cdr error)
759 '("Misplaced t or `otherwise' clause"))))))) 761 '("Misplaced t or `otherwise' clause")))))))
760 762
763(ert-deftest cl-case-warning ()
764 "Test that `cl-case' and `cl-ecase' warn about suspicious
765constructs."
766 (pcase-dolist (`(,case . ,message)
767 `((nil . "Case nil will never match")
768 ('nil . ,(concat "Case 'nil will match `quote'. "
769 "If that's intended, write "
770 "(nil quote) instead. "
771 "Otherwise, don't quote `nil'."))
772 ('t . ,(concat "Case 't will match `quote'. "
773 "If that's intended, write "
774 "(t quote) instead. "
775 "Otherwise, don't quote `t'."))
776 ('foo . ,(concat "Case 'foo will match `quote'. "
777 "If that's intended, write "
778 "(foo quote) instead. "
779 "Otherwise, don't quote `foo'."))
780 (#'foo . ,(concat "Case #'foo will match "
781 "`function'. If that's "
782 "intended, write (foo function) "
783 "instead. Otherwise, don't "
784 "quote `foo'."))))
785 (dolist (macro '(cl-case cl-ecase))
786 (let ((form `(,macro val (,case 1))))
787 (ert-info ((prin1-to-string form) :prefix "Form: ")
788 (ert-with-message-capture messages
789 (macroexpand form)
790 (should (equal messages
791 (concat "Warning: " message "\n")))))))))
792
761;;; cl-macs-tests.el ends here 793;;; cl-macs-tests.el ends here