Back to Scala3

E030: Match Case Unreachable

docs/_docs/reference/error-codes/E030.md

3.8.41.2 KB
Original Source

E030: Match Case Unreachable

This warning is emitted when a case in a pattern match expression can never be reached because it is shadowed by a previous case that matches all the same values.

Unreachable code is typically a sign of a logic error. You should review the order of your cases or remove the redundant case.


Example

scala
def example(x: Int): String = x match
  case _ => "any"
  case 1 => "one"

Warning

scala
-- [E030] Match case Unreachable Warning: example.scala:3:7 --------------------
3 |  case 1 => "one"
  |       ^
  |       Unreachable case

Solution

scala
// Reorder cases - put specific patterns before general ones
def example(x: Int): String = x match
  case 1 => "one"
  case _ => "any"
scala
// Alternative: remove the unreachable case if it's not needed
def example(x: Int): String = x match
  case _ => "any"
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>