vseloved / rutils

Radical Utilities for Common Lisp
Other
246 stars 36 forks source link

Style warning in SBCL (unused variable) using DOPLIST #65

Open filipencopav opened 2 years ago

filipencopav commented 2 years ago

Code to reproduce:

(defparameter *pandoc-vars*
  '(:mainfont "Liberation Sans"
     :documentclass "report"
     :geometry (:margin "2cm")))

(rtl:doplist (key val *pandoc-vars*)
  (princ (list val key)))

Warning:

; in: RUTILS.LIST:DOPLIST (KEY VAL *PANDOC-VARS*)
;     (DESTRUCTURING-BIND
;         (KPTB::KEY KPTB::VAL &REST #:G822)
;         #:G821
;       (PRINC (LIST KPTB::VAL KPTB::KEY)))
; ==>
;   (LET* ((#:G2
;           (SB-C::CHECK-DS-LIST/&REST #:G821 2 2
;                                      '(KPTB::KEY KPTB::VAL &REST #:G822)))
;          (KPTB::KEY (POP #:G2))
;          (KPTB::VAL (POP #:G2))
;          (#:G822 #:G2))
;     (PRINC (LIST KPTB::VAL KPTB::KEY)))
; 
; caught STYLE-WARNING:
;   The variable #:G822 is defined but never used.
; 
; compilation unit finished
;   caught 1 STYLE-WARNING condition
filipencopav commented 2 years ago

Found that a variable for holding the rest of the list is created in destructuring-bind, but not used. destructuring-bind works well in this scenario, i don't know if there's a way to tell it to discard the rest of the list. If there is, then it's possible to do that, otherwise, maybe use a simpler LET form?

(defmacro doplist ((key val plist &optional rez) &body body)
  "Like DOLIST but iterate over 2 items of the PLIST at once,
   onsidered KEY and VAL. Asserts proper PLIST."
  (once-only (plist)
    (with-gensyms (tail)
      `(progn
         (do ((,tail ,plist (cddr ,tail)))
             ((null ,tail) ,rez)
           (destructuring-bind (,key ,val &rest ,(gensym)) ,tail
;                                              ^
;                                              |
;                                             here
             ,@body))))))
(defmacro doplist ((key val plist &optional rez) &body body)
  "Like DOLIST but iterate over 2 items of the PLIST at once,
   onsidered KEY and VAL. Asserts proper PLIST."
  (rtl:once-only (plist)
    (rtl:with-gensyms (tail)
      `(progn
         (do ((,tail ,plist (cddr ,tail)))
             ((null ,tail) ,rez)
           (let ((,key (first ,tail))
                 (,val (second ,tail)))
             ,@body))))))
jcguu95 commented 2 months ago

We only need to add (declare (ignorable ,rest)). A fix has been proposed in PR #72 .