Reports variable definitions that are explicitly initialized with null
.
There are 3 different cases of var v: T = null
:
v
is an optional valueOption[T]
type, which is idiomatic and type safev
is an uninitialized value (a value must be assigned)_
(Scala 2) or scala.compiletime.uninitialized
(Scala 3) as initializer,
which is clear, concise, and platform-independentnull
value)Two quickfixes will be offered:
null
with _
Option
and initialize it with None
Example:
class Test {
var optional: String = null
var uninit: String = null
uninit = "initialized later"
}
After the quick-fixes are applied:
class Test {
var optional: Option[String] = None
var uninit: String = _
uninit = "initialized later"
}