emacs-diffs
[Top][All Lists]
Advanced

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

master 90a543e 2/2: Decrease code duplication in byte-compiler free-vars


From: Stefan Kangas
Subject: master 90a543e 2/2: Decrease code duplication in byte-compiler free-vars warning
Date: Mon, 30 Nov 2020 17:07:21 -0500 (EST)

branch: master
commit 90a543e630012cc58c175d5bf3ffd42bb156c6b6
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>

    Decrease code duplication in byte-compiler free-vars warning
    
    * lisp/emacs-lisp/bytecomp.el
    (byte-compile-free-vars-warn): New defun extracted from...
    (byte-compile-variable-ref, byte-compile-variable-set): ...here.
---
 lisp/emacs-lisp/bytecomp.el | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index 9ece8ec..a20f363 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -3432,6 +3432,27 @@ for symbols generated by the byte compiler itself."
   (push var byte-compile-bound-variables)
   (byte-compile-dynamic-variable-op 'byte-varbind var))
 
+(defun byte-compile-free-vars-warn (var &optional assignment)
+  "Warn if symbol VAR refers to a free variable.
+VAR must not be lexically bound.
+If optional argument ASSIGNMENT is non-nil, this is treated as an
+assignment (i.e. `setq'). "
+  (unless (or (not (byte-compile-warning-enabled-p 'free-vars var))
+              (boundp var)
+              (memq var byte-compile-bound-variables)
+              (memq var (if assignment
+                            byte-compile-free-assignments
+                          byte-compile-free-references)))
+    (let* ((varname (prin1-to-string var))
+           (desc (if assignment "assignment" "reference"))
+           (suggestions (help-uni-confusable-suggestions varname)))
+      (byte-compile-warn "%s to free variable `%s'%s"
+                         desc varname
+                         (if suggestions (concat "\n  " suggestions) "")))
+    (push var (if assignment
+                  byte-compile-free-assignments
+                byte-compile-free-references))))
+
 (defun byte-compile-variable-ref (var)
   "Generate code to push the value of the variable VAR on the stack."
   (byte-compile-check-variable var 'reference)
@@ -3440,15 +3461,7 @@ for symbols generated by the byte compiler itself."
        ;; VAR is lexically bound
         (byte-compile-stack-ref (cdr lex-binding))
       ;; VAR is dynamically bound
-      (unless (or (not (byte-compile-warning-enabled-p 'free-vars var))
-                 (boundp var)
-                 (memq var byte-compile-bound-variables)
-                 (memq var byte-compile-free-references))
-        (let* ((varname (prin1-to-string var))
-               (suggestions (help-uni-confusable-suggestions varname)))
-          (byte-compile-warn "reference to free variable `%s'%s" varname
-                             (if suggestions (concat "\n  " suggestions) "")))
-       (push var byte-compile-free-references))
+      (byte-compile-free-vars-warn var)
       (byte-compile-dynamic-variable-op 'byte-varref var))))
 
 (defun byte-compile-variable-set (var)
@@ -3459,15 +3472,7 @@ for symbols generated by the byte compiler itself."
        ;; VAR is lexically bound.
         (byte-compile-stack-set (cdr lex-binding))
       ;; VAR is dynamically bound.
-      (unless (or (not (byte-compile-warning-enabled-p 'free-vars var))
-                 (boundp var)
-                 (memq var byte-compile-bound-variables)
-                 (memq var byte-compile-free-assignments))
-        (let* ((varname (prin1-to-string var))
-               (suggestions (help-uni-confusable-suggestions varname)))
-          (byte-compile-warn "assignment to free variable `%s'%s" varname
-                             (if suggestions (concat "\n  " suggestions) "")))
-       (push var byte-compile-free-assignments))
+      (byte-compile-free-vars-warn var t)
       (byte-compile-dynamic-variable-op 'byte-varset var))))
 
 (defmacro byte-compile-get-constant (const)



reply via email to

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