From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#48294: Use 'with-current-buffer' byte-compiler warning seems wrong Date: Sun, 09 May 2021 11:05:47 +0300 Message-ID: <83k0o88h7o.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39870"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 48294@debbugs.gnu.org To: rswgnu@gmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 09 10:06:23 2021 Return-path: Envelope-to: geb-bug-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 1lfeSF-000AGP-Jm for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 09 May 2021 10:06:23 +0200 Original-Received: from localhost ([::1]:54972 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lfeSE-0003ei-EZ for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 09 May 2021 04:06:22 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42320) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lfeRv-0003eA-Bu for bug-gnu-emacs@gnu.org; Sun, 09 May 2021 04:06:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:43369) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lfeRu-0003cN-I3 for bug-gnu-emacs@gnu.org; Sun, 09 May 2021 04:06:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lfeRu-0008Dk-Di for bug-gnu-emacs@gnu.org; Sun, 09 May 2021 04:06:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 09 May 2021 08:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48294 X-GNU-PR-Package: emacs Original-Received: via spool by 48294-submit@debbugs.gnu.org id=B48294.162054755431594 (code B ref 48294); Sun, 09 May 2021 08:06:02 +0000 Original-Received: (at 48294) by debbugs.gnu.org; 9 May 2021 08:05:54 +0000 Original-Received: from localhost ([127.0.0.1]:54915 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lfeRm-0008DR-9H for submit@debbugs.gnu.org; Sun, 09 May 2021 04:05:54 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:60150) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lfeRk-0008DE-Ih; Sun, 09 May 2021 04:05:52 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:55662) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lfeRf-0003Sz-CD; Sun, 09 May 2021 04:05:47 -0400 Original-Received: from 84.94.185.95.cable.012.net.il ([84.94.185.95]:2157 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lfeRe-0006kO-NY; Sun, 09 May 2021 04:05:47 -0400 In-Reply-To: (message from Robert Weiner on Sat, 8 May 2021 16:14:09 -0400) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:206045 Archived-At: tags 48294 notabug thanks > From: Robert Weiner > Date: Sat, 8 May 2021 16:14:09 -0400 > > I get a lot of these byte-compile warnings in my Elisp code: > > hbut.el:683:26:Warning: Use ‘with-current-buffer’ rather than > save-excursion+set-buffer > > but since with-current-buffer does not save the value of point, it is > not a valid substitute for save-excursion and should not be suggested. > Evaluate the two samples below and you will see that they are not > equivalent. If I am correct, I'd like this suggestion disabled. Thanks. -- rsw > > (save-excursion > (set-buffer (current-buffer)) > (forward-char 20)) > > (with-current-buffer (current-buffer) > (forward-char 20)) The ELisp manual says about this: Because ‘save-excursion’ only saves point for the buffer that was current at the start of the excursion, any changes made to point in other buffers, during the excursion, will remain in effect afterward. This frequently leads to unintended consequences, so the byte compiler warns if you call ‘set-buffer’ during an excursion: Warning: Use ‘with-current-buffer’ rather than save-excursion+set-buffer To avoid such problems, you should call ‘save-excursion’ only after setting the desired current buffer, as in the following example: (defun append-string-to-buffer (string buffer) "Append STRING to the end of BUFFER." (with-current-buffer buffer (save-excursion (goto-char (point-max)) (insert string)))) I believe this example shows how to solve your problem.