Why does EK9 reject redundant empty checks on collections?

← Code Quality · Ref: Q820

EK9 detects redundant empty checks using data flow analysis. A freshly created collection is always empty, so checking 'if items empty' immediately after creation is redundant — the condition is always true.

E08092: REDUNDANT EMPTY CHECK

Checking 'empty' on a provably empty collection:

  items <- List() of String
  if items empty           // ALWAYS true — just created
    result: "empty"        // this always executes

E08093: NEVER EMPTY CHECK

Checking 'not empty' on a provably empty collection:

  items <- List() of String
  if items not empty       // ALWAYS false — just created
    result: "has items"    // this never executes (dead code)

WHEN CHECKS ARE VALID

After items have been added or the collection comes from a parameter:

  items <- List() of String
  items += "hello"
  if items empty           // valid — state changed since creation
    result: "empty"

See Q310 for code quality overview. See Q734 for tautology detection.

Example

defines module qa.quality.empty.check

  defines function

    checkCollection()
      -> greeting as String
      <- result as String: String()

      items <- List() of String
      items += greeting
      if items empty
        result: "empty"
      else
        result: "has items"

  defines program

    EmptyCheckDemo()
      stdout <- Stdout()

      result <- checkCollection("hello")
      stdout.println(`Collection: ${result}`)

Common mistakes

E08092 — Reassigning items to a freshly-created empty list immediately before 'if items empty' makes the check provably always true, so the compiler flags a redundant empty check. See ek9 -h E08092 for details.

Incorrect:

      items += greeting
      items := List() of String

Correct:

      items += greeting
Other ways to ask this
  • What is E08092 REDUNDANT_EMPTY_CHECK?
  • What is E08093 NEVER_EMPTY_CHECK?
  • Why does EK9 warn about checking if a new list is empty?

Coming from another language?

Java: no detection — redundant isEmpty() compiles silently. Python: no detection. Rust: no detection. Go: no detection. EK9: compile-time error for provably redundant or impossible empty checks.

Keywords: E08093, E08092, collection, code, empty, list, redundant, dead, dict, tautology