Re: Compiler invalid reporting warning. An Emacs 30 compiler limitation/bug?

[email protected]
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Michael Heerdegen via Users list for the GNU Emacs text
editor <[email protected]> writes:

> Pierre Rouleau <[email protected]> writes:
>
>> Hi all,
>>
>> Emacs 30.2.50 reports an unused lexical variable warning
>> for code where the variable is used inside a cl-letf macro.
>> [...]
>>     (let ((kill-ring nil)
>>           (captured-message nil))  ; <<<--- WARNING: Unused lexical
>> variable ‘captured-message’
>>       (cl-letf (((symbol-function 'message)
>>                  (lambda (fmt &rest args)
>>                    (setq captured-message ; <<<---- but it's used here!
>>                          (apply #'format fmt args)))))
>>         (pel-elcode-print-properties-of-sexp-at-point))
>>       (should (equal (car kill-ring)
>>                      "(declare (pure t) (side-effect-free error-free))")))))
>
> Your variable is initialized and then its binding is changed, but the
> value is indeed never used.  The warning is correct, these parts of the
> code will not have any effect.
>

Also, if you were capturing a message so that your function
‘(pel-elcode-print-properties-of-sexp-at-point)’ could
access it, then you might need to use dynamic binding (the
default in Emacs Lisp) instead of lexical binding.  You
could try adding the following text as the first line in
your source file and then seeing whether you get the
behavior that you want:

;; -*- lexical-binding: nil; -*-

See also (info "(elisp) Selecting Lisp Dialect")

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.