aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHelmut Eller2023-08-03 08:33:40 +0200
committerMattias Engdegård2023-08-08 18:23:00 +0200
commit3e79fd3d4e810c2ef4cf9925a747c93e036fddca (patch)
treea978150f0fbeca9823e3d89d54ea1886558cef65
parentefb3ef0fe07a1fe8c713921ceba74f476c8aa40b (diff)
downloademacs-3e79fd3d4e810c2ef4cf9925a747c93e036fddca.tar.gz
emacs-3e79fd3d4e810c2ef4cf9925a747c93e036fddca.zip
Check keyword args of make-process
The functions make-process and make-network-process have many keyword args and it's easy to misspell some of them. Use a compiler macro to warn about some possible mistakes. * lisp/emacs-lisp/bytecomp.el (bytecomp--check-keyword-args): New helper. (make-process, make-network-process): Define a compiler macro that performs some checks but doesn't anything else. * test/lisp/emacs-lisp/bytecomp-tests.el: Add some tests. * test/lisp/emacs-lisp/bytecomp-resources/: (warn-make-process-missing-keyword-arg.el, warn-make-process-missing-keyword-value.el, warn-make-process-repeated-keyword-arg.el, warn-make-process-unknown-keyword-arg.el): New test files
-rw-r--r--lisp/emacs-lisp/bytecomp.el61
-rw-r--r--test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-arg.el3
-rw-r--r--test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-value.el3
-rw-r--r--test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repeated-keyword-arg.el3
-rw-r--r--test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unknown-keyword-arg.el4
-rw-r--r--test/lisp/emacs-lisp/bytecomp-tests.el16
6 files changed, 90 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index f6ba6ff9ea0..0df7b0bfe2a 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -5782,6 +5782,67 @@ and corresponding effects."
5782 form ; arity error 5782 form ; arity error
5783 `(forward-word (- (or ,arg 1))))) 5783 `(forward-word (- (or ,arg 1)))))
5784 5784
5785(defun bytecomp--check-keyword-args (form arglist allowed-keys required-keys)
5786 (let ((fun (car form)))
5787 (cl-flet ((missing (form keyword)
5788 (byte-compile-warn-x
5789 form
5790 "`%S´ called without required keyword argument %S"
5791 fun keyword))
5792 (unrecognized (form keyword)
5793 (byte-compile-warn-x
5794 form
5795 "`%S´ called with unknown keyword argument %S"
5796 fun keyword))
5797 (duplicate (form keyword)
5798 (byte-compile-warn-x
5799 form
5800 "`%S´ called with repeated keyword argument %S"
5801 fun keyword))
5802 (missing-val (form keyword)
5803 (byte-compile-warn-x
5804 form
5805 "missing value for keyword argument %S"
5806 keyword)))
5807 (let* ((seen '())
5808 (l arglist))
5809 (while (consp l)
5810 (let ((key (car l)))
5811 (cond ((and (keywordp key) (memq key allowed-keys))
5812 (cond ((memq key seen)
5813 (duplicate l key))
5814 (t
5815 (push key seen))))
5816 (t (unrecognized l key)))
5817 (when (null (cdr l))
5818 (missing-val l key)))
5819 (setq l (cddr l)))
5820 (dolist (key required-keys)
5821 (unless (memq key seen)
5822 (missing form key))))))
5823 form)
5824
5825(put 'make-process 'compiler-macro
5826 #'(lambda (form &rest args)
5827 (bytecomp--check-keyword-args
5828 form args
5829 '(:name
5830 :buffer :command :coding :noquery :stop :connection-type
5831 :filter :sentinel :stderr :file-handler)
5832 '(:name :command))))
5833
5834(put 'make-network-process 'compiler-macro
5835 #'(lambda (form &rest args)
5836 (bytecomp--check-keyword-args
5837 form args
5838 '(:name
5839 :buffer :host :service :type :family :local :remote :coding
5840 :nowait :noquery :stop :filter :filter-multibyte :sentinel
5841 :log :plist :tls-parameters :server :broadcast :dontroute
5842 :keepalive :linger :oobinline :priority :reuseaddr :bindtodevice
5843 :use-external-socket)
5844 '(:name :service))))
5845
5785(provide 'byte-compile) 5846(provide 'byte-compile)
5786(provide 'bytecomp) 5847(provide 'bytecomp)
5787 5848
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-arg.el
new file mode 100644
index 00000000000..9369e78ff54
--- /dev/null
+++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-arg.el
@@ -0,0 +1,3 @@
1;;; -*- lexical-binding: t -*-
2(defun foo ()
3 (make-process :name "ls"))
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-value.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-value.el
new file mode 100644
index 00000000000..4226349afef
--- /dev/null
+++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-missing-keyword-value.el
@@ -0,0 +1,3 @@
1;;; -*- lexical-binding: t -*-
2(defun foo ()
3 (make-process :name "ls" :command))
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repeated-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repeated-keyword-arg.el
new file mode 100644
index 00000000000..18250f14ee9
--- /dev/null
+++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-repeated-keyword-arg.el
@@ -0,0 +1,3 @@
1;;; -*- lexical-binding: t -*-
2(defun foo ()
3 (make-process :name "ls" :command "ls" :name "ls"))
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unknown-keyword-arg.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unknown-keyword-arg.el
new file mode 100644
index 00000000000..4721035780b
--- /dev/null
+++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-make-process-unknown-keyword-arg.el
@@ -0,0 +1,4 @@
1;;; -*- lexical-binding: t -*-
2(defun foo ()
3 (make-process :name "ls" :command "ls"
4 :coding-system 'binary))
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 19e08e8d199..26325c1ef11 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -1204,6 +1204,22 @@ byte-compiled. Run with dynamic binding."
1204 "nowarn-inline-after-defvar.el" 1204 "nowarn-inline-after-defvar.el"
1205 "Lexical argument shadows" 'reverse) 1205 "Lexical argument shadows" 'reverse)
1206 1206
1207(bytecomp--define-warning-file-test
1208 "warn-make-process-missing-keyword-arg.el"
1209 "called without required keyword argument :command")
1210
1211(bytecomp--define-warning-file-test
1212 "warn-make-process-unknown-keyword-arg.el"
1213 "called with unknown keyword argument :coding-system")
1214
1215(bytecomp--define-warning-file-test
1216 "warn-make-process-repeated-keyword-arg.el"
1217 "called with repeated keyword argument :name")
1218
1219(bytecomp--define-warning-file-test
1220 "warn-make-process-missing-keyword-value.el"
1221 "missing value for keyword argument :command")
1222
1207 1223
1208;;;; Macro expansion. 1224;;;; Macro expansion.
1209 1225