From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: How to avoid compiler warning `unused lexical variable' for `dolist' or `dotimes'? Date: Fri, 08 Jan 2021 00:13:44 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29846"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:Fn8zSIFlwa2iykwpTFVJpMTkw2s= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jan 08 06:14:16 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxk6K-0007eV-De for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jan 2021 06:14:16 +0100 Original-Received: from localhost ([::1]:60596 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kxk6J-0006Wr-FD for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 08 Jan 2021 00:14:15 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59664) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kxk5z-0006Wb-M1 for help-gnu-emacs@gnu.org; Fri, 08 Jan 2021 00:13:55 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]:57978) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kxk5y-0000J6-5S for help-gnu-emacs@gnu.org; Fri, 08 Jan 2021 00:13:55 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1kxk5u-0007BQ-LU for help-gnu-emacs@gnu.org; Fri, 08 Jan 2021 06:13:50 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.248, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:127090 Archived-At: > 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