E129: Pure Expression In Statement Position

This warning is emitted when a pure expression (one without side effects) is used in statement position where its result is not used.

A pure expression does nothing in statement position because it has no side effect and its result is not assigned to a variable or returned. Such expressions can be safely removed without changing the program's semantics, which often indicates a programming error such as a missing assignment or function call.


Example

def example(): Unit =
  1
  ()

Warning

-- [E129] Potential Issue Warning: example.scala:2:2 ---------------------------
2 |  1
  |  ^
  |  A pure expression does nothing in statement position
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The pure expression 1 doesn't have any side effect and its result is not assigned elsewhere.
  | It can be removed without changing the semantics of the program. This may indicate an error.
   -----------------------------------------------------------------------------

Solution

// Remove the unused pure expression
def example(): Unit =
  ()
// Alternative: Assign the value to a variable if it was intended to be used
def example(): Int =
  val x = 1
  x + 1
// Alternative: If the expression was meant to be returned, remove trailing statements
def example(): Int =
  1