Skip to content

Missing "unreachable code" warning when adding case after unfalsifiable extractor #4225

Closed
@smarter

Description

@smarter

The following code produces no warning, even though the case _ is unreachable since Bar returns an instance of Some (scalac correctly produces a warning here):

object Bar {
  def unapply(x: Int): Some[Int] =
    Some(0)
}

object Test {
  def test(x: Int) =
    x match {
      case Bar(a) => a
      case _ => x // should be unreachable
    }
}

Interestingly, if we replace Int by a sealed trait, we do produce a warning:

sealed trait Base
case class One() extends Base
object Foo {
  def unapply(x: Base): Some[Int] =
    Some(0)
}

object Test {
  def test(x: Base) =
    x match {
      case Foo(a) => a
      case _ => x
    }
}
-- [E030] Match case Unreachable Warning: try/some.scala:12:13 -----------------
12 |      case _ => x
   |             ^^^^
   |             unreachable code

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions