SBCL warning that a variable is defined but never used
Asked Answered
B

1

11

I'm getting a warning from the sbcl compiler, that a variable has been defined but is not used. And the compiler is right. I want to get rid of the warning, but don't know how to do it. Here is an example:

(defun worker-1 (context p)
  ;; check context (make use of context argument)
  (if context
      (print p)))

 (defun worker-2 (context p)
   ;; don't care about context
   ;; will throw a warning about unused argument
   (print p))

 ;;
 ;; calls a given worker with context and p
 ;; doesn't know which arguments will be used by the
 ;; implementation of the called worker
 (defun do-cmd (workerFn context p)
   (funcall workerFn context p))

 (defun main ()
    (let ((context ()))
     (do-cmd #'worker-1 context "A")
     (do-cmd #'worker-2 context "A")))

The do-cmd-function expects worker-functions that implement a specific interface f(context p).

The sbcl compiler throws the following warning:

in: DEFUN WORKER-2
;     (DEFUN WORKER-2 (CONTEXT P) (PRINT P))
;
; caught STYLE-WARNING:
 ;   The variable CONTEXT is defined but never used.
;
; compilation unit finished
;   caught 1 STYLE-WARNING condition
Blockish answered 4/7, 2015 at 22:56 Comment(0)
P
21

You need to declare that the parameter is intentionally ignored.

(defun worker-2 (context p)
  (declare (ignore context))
  (print p))

ignore will also signal a warning if you do use the variable. To suppress warnings in both cases, you can use the declaration ignorable, but this should only be used in macros and other such cases where it's not possible to determine whether the variable will be used at the point of its declaration.

In case you are not yet familiar with declare, note that it is not an operator, and instead can only appear in certain locations; in particular, it must be located before all forms in the defun body, though it can be either above or below a documentation string.

Periapt answered 5/7, 2015 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.