Why does EK9 reject unused variables?

← Data Flow Safety · Ref: Q789

EK9 requires that every declared variable is used after its declaration. An unused variable is dead code — it was either left over from refactoring, or the developer forgot to use it.

WHY UNUSED VARIABLES ARE ERRORS

1. Dead code obscures intent — readers wonder why the variable exists
2. Refactoring remnants — code was changed but cleanup was incomplete
3. Possible bug — the developer meant to use it but used a different name

THIS EXAMPLE

The greet() function declares greeting and uses it in the return. Every variable serves a purpose.

COMMON FIX

1. Remove the unused variable if it is not needed
2. Use it in subsequent code if it was accidentally forgotten
3. If the value is needed for side effects only, assign to a used variable

See Q785 for declaration order. See Q22 for variable declarations.

Example

defines module qa.dataflowsafety.unusedvariable

  defines function

    greet() as pure
      -> name as String
      <- rtn as String?

      greeting <- "hello"
      rtn: `${greeting} ${name}`

  defines program

    ShowGreeting()
      stdout <- Stdout()
      stdout.println(greet("world"))

Common mistakes

E08090 — The variable 'unused' is declared but never referenced after assignment. Remove it or use it. See ek9 -h E08090 for details.

Incorrect:

      greeting <- "hello"
      unused <- "forgotten"

Correct:

      greeting <- "hello"
Other ways to ask this
  • What triggers E08090 not referenced?
  • How do I fix an unused variable error in EK9?
  • Why must every variable be used after declaration?

Coming from another language?

Java: unused local variables are warnings, not errors. Python: no enforcement, linters can detect. Rust: warnings for unused variables, prefix with _ to suppress. Kotlin: warnings only. Go: unused variables are compile errors (same as EK9). EK9: unused variables are compile errors, like Go.

Keywords: unused, dead, refactoring, code, variable, E08090, declared, referenced