E11018: unused captured variable in dynamic function or class

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

What went wrong

A variable is captured by a closure/dynamic function but never used inside that function's body.

Worked examples

Compilable examples demonstrating this error and the correct fix:

How to fix it

  1. Remove the unused variable from enclosing scope if not needed
  2. Or use the variable in the dynamic function body
  3. Or restructure to avoid capturing what's not needed

Common causes

  • Copy-paste error - captured variable from template not needed
  • Refactoring removed usage but left capture
  • Variable intended for use but forgotten

See also

  • E11018: unused captured variable (this error)
  • E08090: not referenced anywhere (unused variable)
  • E08091: unused parameter

Examples

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

Fails with E11018: unused captured variable in dynamic function or class

#!ek9
defines module error.e11018.bad

  defines program
    ShowUnusedCapture()

      stdout <- Stdout()

      //'second' is captured but the hand-written 'operator ?' only checks 'first'.
      pair <- Pair(first: "hello", second: "world") as class
        override operator ? as pure
          <- rtn as Boolean: first?

      stdout.println(`${pair?}`)

Fixed: Use every captured value - here the generated operator ? checks them all

#!ek9
defines module error.e11018.good

  defines program
    ShowPositive()

      stdout <- Stdout()

      //Both captures are read implicitly by the synthesised 'default operator ?'.
      pair <- Pair(first: "hello", second: "world") as class
        default operator ?

      stdout.println(`${pair?}`)

Questions and answers

These Q&A entries show E11018 and its fix: