Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBCL warning that a variable is defined but never used

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
like image 801
Ollimaus Avatar asked Jul 04 '15 22:07

Ollimaus


1 Answers

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.

like image 139
Kevin Reid Avatar answered Nov 07 '22 01:11

Kevin Reid