Why does EK9 reject 'not empty' on a freshly created collection?

← Code Quality · Ref: Q825

EK9 detects when 'not empty' is checked on a collection that is provably empty. The condition is always false, making the body dead code.

THE PATTERN

  items <- List() of String
  if items not empty       // ALWAYS false — list just created
    process(items)         // dead code — never executes

The compiler knows the list was just created with no items added, so 'not empty' can never be true at this point.

VALID USE

After items have been added, the check is meaningful:

  items <- List() of String
  items += value
  if items not empty       // valid — items were added
    process(items)

See Q820 for redundant empty checks (E08092). See Q734 for tautology detection.

Example

defines module qa.quality.never.empty

  defines function

    processIfPresent()
      -> inputValue as String
      <- result as String: "nothing"

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

  defines program

    NeverEmptyDemo()
      stdout <- Stdout()
      result <- processIfPresent("hello")
      stdout.println(`Result: ${result}`)

Common mistakes

E08093 — Without adding items, the list is provably empty. 'not empty' is always false — the body is dead code. See ek9 -h E08093 for details.

Incorrect:

      //items += inputValue
      if items not empty

Correct:

      items += inputValue
      if items not empty
Other ways to ask this
  • What is E08093 NEVER_EMPTY_CHECK?
  • Why is my 'not empty' check dead code in EK9?
  • How does EK9 detect impossible empty checks?

Coming from another language?

Java: no detection. Python: no detection. Rust: no detection. Go: no detection. EK9: compile-time error for provably impossible 'not empty' checks using data flow analysis.

Keywords: dead, list, dict, E08093, never, empty, collection, tautology, code