Subject: [PATCH] (setq-default,setopt): Warn about unknown vars.

From: Eshel Yaron <[email-masked]>
Date: Sun, 10 Aug 2025 13:05:08 +0200

Tags: patch

Hi,

This patch makes the byte-compiler (and by extension, Flymake), emit
warnings when setq-default and setopt are used to set the value of
unknown variables, similarly to the warnings we get with setq.

Ideally we should do something similar for setq-local, but AFAICT that
requires a different solution due to different implementation details.

>From 36bd5ddae0c4f02081b6171ceb59f2c23605b440 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Sun, 10 Aug 2025 12:47:00 +0200
Subject: [PATCH] (setq-default,setopt): Warn about unknown vars.

---
 lisp/emacs-lisp/bytecomp.el | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index cbfca753b30..1a7f10b739b 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -4390,6 +4390,8 @@ byte-compile-set-default
     (if (eq (car-safe varexp) 'quote)
         ;; If the varexp is constant, check the var's name.
         (let ((var (car-safe (cdr varexp))))
+          (when (symbolp var)
+            (byte-compile-free-vars-warn var var t))
           (and (or (not (symbolp var))
 	           (macroexp--const-symbol-p var t))
                (byte-compile-warning-enabled-p 'constants
@@ -4401,6 +4403,8 @@ byte-compile-set-default
 	        var))))
     (byte-compile-normal-call form)))
 
+(byte-defop-compiler-1 setopt--set byte-compile-set-default)
+
 (defun byte-compile-quote (form)
   (byte-compile-constant (car (cdr form))))
 
-- 
2.47.2