E175: Value Discarding
This warning occurs when a non-Unit expression's value is discarded (not used). This often indicates a programming error where a return value is accidentally ignored.
This warning is enabled with the -Wvalue-discard compiler flag and helps catch bugs where important values like error results or updated collections are not handled.
Example
//> using options -Wvalue-discard
import scala.collection.mutable
def example: Unit = {
mutable.Set.empty[String].remove("")
}
Error
-- [E175] Potential Issue Warning: example.scala:6:34 --------------------------
6 | mutable.Set.empty[String].remove("")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|discarded non-Unit value of type Boolean. Add `: Unit` to discard silently.
Solution
//> using options -Wvalue-discard
import scala.collection.mutable
def example: Unit = {
val set = mutable.Set.empty[String]
// Use the return value
val wasRemoved: Boolean = set.remove("")
if wasRemoved then println("Removed")
}
//> using options -Wvalue-discard
import scala.collection.mutable
def example: Unit = {
// Explicitly discard by adding : Unit
mutable.Set.empty[String].remove(""): Unit
}
In this article