Reports variable definitions that are explicitly initialized with null.

There are 3 different cases of var v: T = null:

  1. v is an optional value
    → use Option[T] type, which is idiomatic and type safe
  2. v is an uninitialized value (a value must be assigned)
    → use _ (Scala 2) or scala.compiletime.uninitialized (Scala 3) as initializer, which is clear, concise, and platform-independent
  3. It is a performance optimization (you actually use the null value)
    → explicitly suppressed the inspection for the unsafe code

Two quickfixes will be offered:

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"
  }