E08092: empty check is always true based on data flow analysis

Category: E08xxx Pre-IR checks (flow analysis) · Reported in phase: PRE_IR_CHECKS · Classification: REDUNDANT_EMPTY_CHECK · All error codes

What went wrong

This empty check is always true based on data flow analysis. The collection or container is provably empty at this point — either because it was just constructed with no elements, or is an empty Optional/Result.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 820 (Why does EK9 reject redundant empty checks on collections?)

How to fix it

  1. Remove the redundant empty check and unwrap the code
  2. If the collection was just created with no elements, the check is unnecessary
  3. If this is an empty Optional, the check is unnecessary
  4. Check if items were intended to be added before this check

Common causes

  • List/Dict created with no elements and immediately checked for empty
  • Optional created with no value and checked for empty
  • Defensive coding habit from languages where collections might be null

See also

  • E08093: empty check always false (container provably non-empty)
  • E08088: isSet check always true (variable provably SET)
  • E08086: condition always true (comparison-based tautology)

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E08092: empty check is always true based on data flow analysis

#!ek9
defines module error.e08092.bad

  defines function

    checkEmptyList()
      <- result as String: "has items"
      items <- List() of String
      if items empty
        result: "empty"

Fixed: Check a collection whose contents are not known

#!ek9
defines module error.e08092.good

  defines function

    checkEmptyList()
      -> items as List of String
      <- result as String: "has items"
      if items empty
        result: "empty"

Questions and answers

These Q&A entries show E08092 and its fix: