package example
object UnapplyNonExhaustive {
final class Foo(val x: Int)
object Foo {
def unapply(f: Foo): Option[Int] =
Some(f.x)
}
val nonExhaustiveWarning: Unit = new Foo(1) match {
case Foo(x) => println(x)
}
val alsoHere: Unit = {
val Foo(x) = new Foo(1)
println(x)
}
}
With -Xlint, both pattern matches emit a match may not be exhaustive warning with no counter-examples, despite Foo being a final class with no other candidates for the match. Expected behaviour is either no warning, or some counter-examples (even if that is null).
Example output from SBT:
[warn] /home/azl/nonexhaustiveexample/src/main/scala/example/Hello.scala:10:36: match may not be exhaustive.
[warn] val nonExhaustiveWarning: Unit = new Foo(1) match {
[warn] ^
[warn] /home/azl/nonexhaustiveexample/src/main/scala/example/Hello.scala:15:18: match may not be exhaustive.
[warn] val Foo(x) = new Foo(1)
[warn] ^
[warn] two warnings found
Reproduction steps
Scala version: 2.13.12 Java version: 17.0.1
Compile with
-Xlint
.Example SBT project that reproduces: nonexhaustiveexample-frozen.zip
Problem
With
-Xlint
, both pattern matches emit amatch may not be exhaustive
warning with no counter-examples, despiteFoo
being a final class with no other candidates for the match. Expected behaviour is either no warning, or some counter-examples (even if that isnull
).Example output from SBT: