E176: Unused Non-Unit Value
This warning is emitted when a non-Unit value is computed in statement position but never used.
This warning helps identify situations where the result of an expression is silently discarded, which may indicate a programming error such as forgetting to use or assign a computed value.
This warning is only emitted when the -Wnonunit-statement compiler flag is enabled.
Example
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
list.map(_ + 1) // result is unused
println("done")
Error
-- [E176] Potential Issue Warning: example.scala:5:10 --------------------------
5 | list.map(_ + 1) // result is unused
| ^^^^^^^^^^^^^^^
| unused value of type List[Int]
Solution
If the result is intentional, explicitly discard it or use it:
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
val _ = list.map(_ + 1) // explicitly discard
println("done")
Alternatively, use the result:
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
val transformed = list.map(_ + 1)
println(s"Transformed: $transformed")
In this article