E1002#
Unused variable.
This variable is unused by any other part of your code, nor marked with pub
visibility.
Note that this warning might uncover other bugs in your code. For example, if there are two variables in your codebase that has similar name, you might just use the other variable by mistake.
Specifically, if the variable is at the toplevel, and the body of the module contains side effects, the side effects will not happen.
Erroneous example#
let p : Int = {
// ^ Warning: Unused toplevel variable 'p'.
// Note if the body contains side effect, it will not happen.
// Use `fn init { .. }` to wrap the effect.
println("Side effect")
42
}
fn main {
let x = 42 // Warning: Unused variable 'x'
}
Suggestion#
There are multiple ways to fix this warning:
If the variable is indeed useless, you can remove the definition of the variable.
If this variable is at the toplevel (i.e., not local), and is part of the public API of your module, you can add the
pub
keyword to the variable.pub let p = 42
If you made a typo in the variable name, you can rename the variable to the correct name at the use site.
If your code depends on the side-effect of the variable, you can wrap the side-effect in a
fn init
block.fn init { println("Side effect") }
There are some cases where you might want to keep the variable private and
unused at the same time. In this case, you can call ignore()
on the variable
to force the use of it.
let p : Int = {
println("Side effect")
42
}
fn init {
ignore(p)
}
fn main {
let x = 42
ignore(x)
}