E11051: pure method/function returns a value but result is discarded - a pure call with no side effects achieves nothing

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

What went wrong

A pure method or function was called as a standalone statement, but its non-Void return value is discarded. A pure method has NO side effects — the return value is the ONLY reason to call it. This call achieves nothing.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 732 (Why must every constructor, operator, and pure function r...)
  • ek9 -q 777 (Why does EK9 reject discarded operator return values?)

Rationale

The 'as pure' modifier guarantees no mutation, no I/O, no state changes. If the return value is also discarded, the call has zero observable effect. This is always dead code — there is no scenario where this is intentional.

How to fix it

  1. Assign the result: total <- calculateTotal(items)
  2. Use in an expression: if inventory.contains(item)
  3. Use in a guard: if found <- registry.find(userId)

See also

  • E11050: discarded operator return
  • E11052: discarded Result or Optional

Examples

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

Fails with E11051: pure method/function returns a value but result is discarded - a pure call with no side effects achieves nothing

#!ek9
defines module error.e11051.bad

  defines class

    Calculator

      add() as pure
        -> operand as Integer
        <- result as Integer: operand + 1

    TestClass

      testDiscardedPureMethodCall()
        calc <- Calculator()
        calc.add(10)

  defines function

    pureCompute() as pure
      -> input as Integer
      <- result as Integer: input * 2

    testDiscardedPureFunctionCall()
      pureCompute(42)

Fixed: Use the result of a pure call: return it, or assign it and use it

#!ek9
defines module error.e11051.good.fix

  defines class

    Calculator

      add() as pure
        -> operand as Integer
        <- result as Integer: operand + 1

    TestClass

      testDiscardedPureMethodCall()
        <- total as Integer: Calculator().add(10)

  defines function

    pureCompute() as pure
      -> input as Integer
      <- result as Integer: input * 2

    testDiscardedPureFunctionCall()
      <- total as Integer: pureCompute(42)

Fixed: Use the result - discarding is only for a call made for its side effect

#!ek9
defines module error.e11051.good

  defines function

    voidFunction()
      Stdout().println("side effect")

    impureCompute()
      <- result as Integer: 42

    testVoidCallIsOk()
      voidFunction()

    testNonPureCallIsOk()
      impureCompute()

    testAssignedReturnIsOk()
      result <- impureCompute()
      Stdout().println($result)

    testMutatingOperatorIsOk()
      counter <- 0
      counter++
      counter += 5
      Stdout().println($counter)

    testStdoutIsOk()
      Stdout().println("This is fine")

Questions and answers

These Q&A entries show E11051 and its fix:

Found a problem?

If EK9 reported E11051 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.