help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: How to avoid compiler warning `unused lexical variable' for `dolist'


From: Stefan Monnier
Subject: Re: How to avoid compiler warning `unused lexical variable' for `dolist' or `dotimes'?
Date: Fri, 08 Jan 2021 00:13:44 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> To me that means that I need to use workaround because `dotimes' does
> not work well as described in docstring. I need to remember something
> that is nowhere documented.

In which way doesn't it work as documented?
The warning is just that: a warning.  It doesn't affect the actual behavior.

> Either compiler warning is wrong, or docstring is wrong.  One of those
> shall be improved.

AFAIK both are right: you put into the 3rd arg an expression that is
evaluated in a context where a new variable `var` has been added for you
but you don't make use of that variable.  This fact doesn't prevent the
code from being valid and executed correctly, but it is suspicious so it
deserves a warning.

> Users or programmers shall not be directed to avoid using function
> which is described how to be used in their documentation.

"Warning" is not the same as "error".

A warning is emitted when the code is valid but where we think it's
useful to bring the attention to something of which the programmer may
not be aware.  E.g. in this case, the fact that your code has a useless
extra `let` as you can see in the macro-expanded version of the code:

    (macroexpand '(dotimes (var times amount)
                    (setq amount (pct-plus amount percent))))
=>
    (let ((--dotimes-limit-- times)
          (--dotimes-counter-- 0))
      (while (< --dotimes-counter-- --dotimes-limit--)
        (let ((var --dotimes-counter--))
          (setq amount (pct-plus amount percent)))
          (setq --dotimes-counter-- (1+ --dotimes-counter--)))
      (let ((var --dotimes-counter--)) amount))

Many/most of the warnings depend on value judgments which depend on what
is considered "good style", so of course there is bound to be people
who disagree in some cases.

We occasionally also use those warnings to nudge programmers toward
a particular programming style, i.e. with a conscious decision to make
people change their style, which makes it yet more likely that some
people will disagree (at least at first, until they get used to the new
style).  E.g. in early Emacs it was a lot more common to find packages
which relied on non-trivial ways uses of dynamic scoping and undeclared
global vars.  After adding warnings for uses of "free variables" the
style changed over the years, making it possible in Emacs-24 to
introduce lexical scoping such that the vast majority of the code works
unchanged when `lexical-binding` is activated.


        Stefan




reply via email to

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