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

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

Warning

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

Solution

// Reorder cases - put specific patterns before general ones
def example(x: Int): String = x match
  case 1 => "one"
  case _ => "any"
// Alternative: remove the unreachable case if it's not needed
def example(x: Int): String = x match
  case _ => "any"