bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Tino Calancha-2

Write a file '/tmp/test.el' with following content:
--8<-----------------------------cut here---------------start------------->8---
;; -*- lexical-binding: t; -*-

;; Compiles OK
(defun test ()
  (let ((lst (list "foo" 1)))
    (when (cdr lst)
      (equal (cdr lst) 1))))

;; Compiles OK
(defun getval (x) (cdr x))
(defun test3 ()
  (let ((alist (list (cons "foo" 1) (cons "bar" 2))))
    (dolist (x alist)
      (when (getval x)
        (equal (getval x) (alist-get (car x) alist))))))

;; Warning: value returned from (cdr x) is unused
(defun test2 ()
  (let ((alist (list (cons "foo" 1) (cons "bar" 2))))
    (dolist (x alist)
    (when (cdr x)
      (equal (cdr x) (alist-get (car x) alist))))))

--8<-----------------------------cut here---------------end--------------->8---

emacs -Q
M-x byte-compile-file /tmp/test.el RET
I got a warning from test2 func:
Warning: value returned from (cdr x) is unused

But the value is used as the `when' condition, and as `equal'
1st argument.


In GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-08-16
Repository revision: 3305dec5387021791eb09a93df5ab784b2297dc8




Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Glenn Morris-3
Tino Calancha wrote:

> ;; Warning: value returned from (cdr x) is unused
> (defun test2 ()
>   (let ((alist (list (cons "foo" 1) (cons "bar" 2))))
>     (dolist (x alist)
>     (when (cdr x)
>       (equal (cdr x) (alist-get (car x) alist))))))

Isn't this function a no-op?
Eg dolist does not return the last value from the body.
So a smaller example of the same thing is:

(dolist (x '(1))
  (equal (+ x 2) 3))

which returns nil.

> ;; Compiles OK
> (defun getval (x) (cdr x))
> (defun test3 ()
>   (let ((alist (list (cons "foo" 1) (cons "bar" 2))))
>     (dolist (x alist)
>       (when (getval x)
>         (equal (getval x) (alist-get (car x) alist))))))

If getcdr is known to be side-effect-free, you get the same warning.

(eval-when-compile
  (put 'getcdr 'side-effect-free t))


So I think this is a (slightly inaccurate) warning of a real issue.



Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Glenn Morris-3
Glenn Morris wrote:

> If getcdr is known to be side-effect-free, you get the same warning.

s/getcdr/getval



Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Tino Calancha-2
In reply to this post by Glenn Morris-3


On Thu, 17 Aug 2017, Glenn Morris wrote:

> Tino Calancha wrote:
>
>> ;; Warning: value returned from (cdr x) is unused
>> (defun test2 ()
>>   (let ((alist (list (cons "foo" 1) (cons "bar" 2))))
>>     (dolist (x alist)
>>     (when (cdr x)
>>       (equal (cdr x) (alist-get (car x) alist))))))
>
> Isn't this function a no-op?
> Eg dolist does not return the last value from the body.
> So a smaller example of the same thing is:
>
> (dolist (x '(1))
>  (equal (+ x 2) 3))
>
> which returns nil.
Even without the dolist we get such warning:
$> cat /tmp/test.el
;; -*- lexical-binding: t; -*-
(let ((x 1))
   (and (equal (+ x 2) 3)))

M-x byte-compile-file RET /tmp/test RET
Compiling file /tmp/test.el at Fri Aug 18 02:30:59 2017
test.el:2:1:Warning: value returned from (+ x 2) is unused
test.el:2:1:Warning: value returned from (+ x 2) is unused

I used (+ x 2) as 1st argument for `equal'.  What more should i do to
use it? Maybe invite (+ x 2) to the cinema?
I hope (+ x 2) will pay, cinema it's too expensive here.



Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Glenn Morris-3
Tino Calancha wrote:

> ;; -*- lexical-binding: t; -*-
> (let ((x 1))
>   (and (equal (+ x 2) 3)))

This code also does nothing. Compare with:

(defun foo ()
  (let ((x 1))
   (and (equal (+ x 2) 3))))

where the result is used (as the function return) and there is no warning.



Reply | Threaded
Open this post in threaded view
|

bug#28107: 26.0.50; Byte compilation shows an unused var warning for an used variable

Lars Ingebrigtsen
Glenn Morris <[hidden email]> writes:

> Tino Calancha wrote:
>
>> ;; -*- lexical-binding: t; -*-
>> (let ((x 1))
>>   (and (equal (+ x 2) 3)))
>
> This code also does nothing. Compare with:
>
> (defun foo ()
>   (let ((x 1))
>    (and (equal (+ x 2) 3))))
>
> where the result is used (as the function return) and there is no warning.

So I don't think there's a bug here, and I'm closing this bug report.

--
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no