From: Eshel Yaron Date: Fri, 27 Sep 2024 10:18:21 +0000 (+0200) Subject: Warn about unreachable code X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=07a794707914a3a6b56ebb5cd94a48d89f419afa;p=emacs.git Warn about unreachable code --- diff --git a/lisp/dired.el b/lisp/dired.el index 72ce20449b4..b5b4399cc0a 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -973,7 +973,7 @@ marked file, return (t FILENAME) instead of (FILENAME)." (dired-repeat-over-lines ,arg (lambda () - (if ,show-progress (sit-for 0)) + ,@(when show-progress '((sit-for 0))) (setq results (cons ,body results)))) (when (< ,arg 0) (setq results (nreverse results))) @@ -992,7 +992,7 @@ marked file, return (t FILENAME) instead of (FILENAME)." found (not (null next-position))) (while next-position (goto-char next-position) - (if ,show-progress (sit-for 0)) + ,@(when show-progress '((sit-for 0))) (setq results (cons ,body results)) ;; move after last match (goto-char next-position) @@ -1000,8 +1000,8 @@ marked file, return (t FILENAME) instead of (FILENAME)." (set-marker next-position nil) (setq next-position (and (re-search-forward regexp nil t) (point-marker))))) - (if (and ,distinguish-one-marked (= (length results) 1)) - (setq results (cons t results))) + ,@(when distinguish-one-marked + '((if (= (length results) 1) (setq results (cons t results))))) (if found results (unless (eq ,arg 'marked) diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el index dc97080fbc2..363fa0e667f 100644 --- a/lisp/emacs-lisp/byte-opt.el +++ b/lisp/emacs-lisp/byte-opt.el @@ -365,6 +365,11 @@ There can be multiple entries for the same NAME if it has several aliases.") ;; FIXME: We are conservative here: any variable changed in the ;; THEN branch will be barred from substitution in the ELSE ;; branch, despite the branches being mutually exclusive. + (cond + ((byte-compile-nilconstp test) + (byte-compile-warn-x then "Unreachable `then' clause")) + ((and else (byte-compile-trueconstp test)) + (byte-compile-warn-x else "Unreachable `else' clause"))) (let* ((test-opt (byte-optimize-form test nil)) (const (macroexp-const-p test-opt)) ;; Avoid traversing dead branches. @@ -383,6 +388,12 @@ There can be multiple entries for the same NAME if it has several aliases.") (let ((tail exps) (args nil)) (while tail + (when (cdr tail) + (cond + ((and (eq fn 'and) (byte-compile-nilconstp (car tail))) + (byte-compile-warn-x (cdr tail) "Unreachable `and' clause")) + ((and (eq fn 'or) (byte-compile-trueconstp (car tail))) + (byte-compile-warn-x (cdr tail) "Unreachable `or' clause")))) (push (byte-optimize-form (car tail) (and for-effect (null (cdr tail)))) args) @@ -422,21 +433,21 @@ There can be multiple entries for the same NAME if it has several aliases.") (and (not for-effect) form)) (`(condition-case ,var ,exp . ,clauses) - `(,fn ,var ;Not evaluated. + `(,fn ,var ;Not evaluated. ,(byte-optimize-form exp (if (assq :success clauses) (null var) for-effect)) - ,@(mapcar (lambda (clause) - (let ((byte-optimize--lexvars - (and lexical-binding - (if var - (cons (list var t) - byte-optimize--lexvars) - byte-optimize--lexvars)))) - (cons (car clause) - (byte-optimize-body (cdr clause) for-effect)))) - clauses))) + ,@(mapcar (lambda (clause) + (let ((byte-optimize--lexvars + (and lexical-binding + (if var + (cons (list var t) + byte-optimize--lexvars) + byte-optimize--lexvars)))) + (cons (car clause) + (byte-optimize-body (cdr clause) for-effect)))) + clauses))) (`(unwind-protect ,protected-expr :fun-body ,unwind-fun) ;; FIXME: The return value of UNWIND-FUN is never used so we @@ -447,7 +458,7 @@ There can be multiple entries for the same NAME if it has several aliases.") (`(catch ,tag . ,exps) `(,fn ,(byte-optimize-form tag nil) - . ,(byte-optimize-body exps for-effect))) + . ,(byte-optimize-body exps for-effect))) ;; Needed as long as we run byte-optimize-form after cconv. (`(internal-make-closure ,vars ,env . ,rest) @@ -459,8 +470,8 @@ There can be multiple entries for the same NAME if it has several aliases.") (let ((lexvar (assq var byte-optimize--lexvars)) (value (byte-optimize-form expr nil))) (when lexvar - (setcar (cdr lexvar) t) ; Mark variable to be kept. - (setcdr (cdr lexvar) nil) ; Inhibit further substitution. + (setcar (cdr lexvar) t) ; Mark variable to be kept. + (setcdr (cdr lexvar) nil) ; Inhibit further substitution. ;; Cancel substitution of variables aliasing this one. (let ((aliased-vars byte-optimize--aliased-vars)) diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el index a87b77a875e..f34e65feaa4 100644 --- a/lisp/emacs-lisp/bytecomp.el +++ b/lisp/emacs-lisp/bytecomp.el @@ -5912,20 +5912,31 @@ and corresponding effects." (put 'char-before 'compiler-macro #'bytecomp--char-before) (defun bytecomp--char-before (form &optional arg &rest junk-args) (if junk-args - form ; arity error - `(char-after (1- (or ,arg (point)))))) + form ; arity error + `(char-after (1- ,(cond + ((byte-compile-nilconstp arg) '(point)) + ((byte-compile-trueconstp arg) arg) + (t `(or ,arg (point)))))))) (put 'backward-char 'compiler-macro #'bytecomp--backward-char) (defun bytecomp--backward-char (form &optional arg &rest junk-args) (if junk-args - form ; arity error - `(forward-char (- (or ,arg 1))))) + form ; arity error + `(forward-char ;; (- (or ,arg 1)) + (- ,(cond + ((byte-compile-nilconstp arg) 1) + ((byte-compile-trueconstp arg) arg) + (t `(or ,arg 1))))))) (put 'backward-word 'compiler-macro #'bytecomp--backward-word) (defun bytecomp--backward-word (form &optional arg &rest junk-args) (if junk-args - form ; arity error - `(forward-word (- (or ,arg 1))))) + form ; arity error + `(forward-word ;; (- (or ,arg 1)) + (- ,(cond + ((byte-compile-nilconstp arg) 1) + ((byte-compile-trueconstp arg) arg) + (t `(or ,arg 1))))))) (defun bytecomp--check-keyword-args (form arglist allowed-keys required-keys) (let ((fun (car form)))