Max Heiber
Max Heiber

Reputation: 15502

Is there a way to suppress *only* exhaustiveness checking in Scala?

I find myself writing the code like this a lot:


thing match {
   case Case1 ...
   case Case2 ...
   case _ => throw new IllegalStateException(s"unexpected $thing")
}

Sometimes I want a runtime error when the cases don't match. The cases are a form of assertion.

Is there a better way to suppress the exhaustiveness check?

I don't want to use [@unchecked](https://www.scala-lang.org/api/2.12.1/scala/unchecked.html) because that would also disable reachability checking, which I do want.

Upvotes: 2

Views: 866

Answers (1)

Krzysztof Atłasik
Krzysztof Atłasik

Reputation: 22595

You could use nowarn annotation to hide warning:

import scala.annotation.nowarn

@nowarn("msg=not.*?exhaustive")
val r = thing match {
   case Case1 ...
   case Case2 ...
}

It was added in Scala 2.13.2 so if you're using an older version you will need to use to silencer plugin.

Alternatively, with a silencer plugin, you could set up global regex-based suppresion.

Upvotes: 9

Related Questions