E11052: method returns Result or Optional but result is discarded - must always be checked

Category: E11xxx Code quality and IR analysis · Reported in phase: PRE_IR_CHECKS · Classification: DISCARDED_RESULT_OR_OPTIONAL · All error codes

What went wrong

A method returning Result or Optional was called as a standalone statement. The return value — which explicitly signals potential failure or absence — is silently discarded.

RESULT: Discarding a Result means potential errors are silently swallowed. No one ever sees the error: the failure the method reported is lost.

OPTIONAL: Discarding an Optional means the "might not exist" semantic is ignored. The method declared that the outcome is uncertain, and the caller threw away the answer.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 47 (How does the Optional type work in EK9?)
  • ek9 -q 48 (How does the Result type work in EK9?)

Rationale

Rust considers this important enough for a default warning on Result types. C++17 added [[nodiscard]] as a standard attribute. Go's errcheck linter is one of the most commonly adopted linters. All target this exact pattern.

How to fix it

  1. Handle the Result: keep it in a variable and test 'isOk()' before reading 'ok()'
  2. Handle the Optional: keep it and test it with '?' before reading 'get()', or guard on the call directly in the 'if'

The [EXAMPLE] section below shows a Result and an Optional each called and thrown away, then both kept and handled.

See also

  • E11050: discarded operator return
  • E11051: discarded pure method return

Examples

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

Fails with E11052: method returns Result or Optional but result is discarded - must always be checked

#!ek9
defines module error.e11052.bad

  defines function

    riskyOperation()
      <- rtn <- Result("OK", Integer())

    findItem()
      <- rtn <- Optional("Found")

    testDiscardedResult()
      riskyOperation()

    testDiscardedOptional()
      findItem()

Fixed: Check the Result or Optional you are given

#!ek9
defines module error.e11052.good

  defines function

    riskyOperation()
      <- rtn <- Result("OK", Integer())

    findItem()
      <- rtn <- Optional("Found")

    useResult()
      <- rtn as String: "failed"
      outcome <- riskyOperation()
      if outcome.isOk()
        rtn: outcome.ok()

    useOptional()
      <- rtn as String: "missing"
      found <- findItem()
      if found?
        rtn: found.get()

Questions and answers

These Q&A entries show E11052 and its fix: