E030: Match Case Unreachable — 도달할 수 없는 match 케이스예요

E030: Match Case Unreachable — 도달할 수 없는 match 케이스예요

패턴 매치 식의 어떤 case가 결코 도달할 수 없을 때 이 경고가 나와요. 앞선 case가 같은 값을 모두 매치해 버려서 그 뒤의 case가 가려지는(shadow) 상황이에요.

출처: Scala 3 Reference

본문

도달할 수 없는 코드는 대개 로직 에러의 신호예요. case의 순서를 검토하거나, 중복된 case를 제거하는 게 좋아요.

예제 (Example)

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

case _가 모든 값을 매치하므로, 그 뒤의 case 1은 결코 실행될 수 없어요.

경고 메시지 (Warning)

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

해결 방법 (Solution)

구체적인 패턴을 일반적인 패턴보다 앞에 두도록 순서를 바꾸거나, 필요 없다면 도달 불가능한 case를 제거해요.

// 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"

더 알아보기 (Learn more)