help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: How do I remove "reference to free variable" warnings onbuffer-local


From: Drew Adams
Subject: RE: How do I remove "reference to free variable" warnings onbuffer-local variables?
Date: Mon, 9 Nov 2009 09:40:12 -0800

> ,----[ (info "(elisp)Compiler Errors") ]
> |    You can tell the compiler that a function is defined using
> | `declare-function' (*note Declaring Functions::).  Likewise, you can
> | tell the compiler that a variable is defined using `defvar' with no
> | initial value.
> | 
> |    You can suppress the compiler warning for a specific use of an
> | undefined variable VARIABLE by conditionalizing its use on 
> | a `boundp' test, like this:
> | 
> |      (if (boundp 'VARIABLE) ...VARIABLE...)
> `----
> 
> So in your case you could add something like
>   (or (boundp 'some-variable) (defvar some-variable))
> at the top of your file.

I don't think so - but I'm no expert on this.

`(defvar some-variable)' does not assign a value to the variable. It is used
(AFAIK) only to suppress a compiler warning. As such, there is no need and no
reason to put it behind `(boundp 'some-variable)'.

Since it does not assign a value, there is typically also no need to take
special measures for local values vs default value. Just use `(defvar
some-variable)' at the top level of a file, if you want to suppress the compiler
warning generally for that variable.

What that passage from the manual is saying, I think, is that you can use
`boundp' to suppress the warning only locally if you want, "for a specific use
of an undefined variable". In the example you gave, `defvar' doesn't really
constitute a "use" of the variable - or if it does, it is not a use that would
cause a warning.

An example of using `boundp' to suppress a warning would be `(if (boundp 'foo)
foo bar)'. Here, if `foo' is not bound, no warning is issued. If `bar' is
unbound, you will get the warning for `bar', since it is not protected by
`boundp'.

IOW, as an alternative to using `(defvar foo)', which suppresses all warnings
about foo being unbound, you can use `(boundp 'foo)' to suppress the unbound
warning for only a specific use of the variable. This warning suppression via
`boundp' was new with Emacs 22.

Again, I'm no expert on this. Others will no doubt correct me. But I'm pretty
sure that the example you gave is not useful: the `boundp' test is not needed,
and it doesn't do anything, since the "use" of the variable for which the
warning would be suppressed here is simply `(defvar some-variable)', which
doesn't issue a warning anyway.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]