E198: Unused Symbol

E198: Unused Symbol

이 경고는 심볼(변수, 파라미터, import 등)이 선언되었지만 한 번도 사용되지 않을 때 발생해요.

출처: Scala 3 Reference

본문

이 경고는 죽은 코드(dead code)나, 값을 사용하려고 했지만 깜빡 잊은 실수를 찾는 데 도움을 줘요. 경고가 잡아내는 다양한 종류의 미사용 심볼은 다음과 같아요.

  • 사용되지 않는 import
  • 사용되지 않는 지역 정의 (vals, vars, defs)
  • 사용되지 않는 파라미터 (명시적, 암시적)
  • 사용되지 않는 private 멤버
  • 사용되지 않는 패턴 변수

이 경고는 적절한 하위 옵션(예: -Wunused:all)과 함께 -Wunused 컴파일러 플래그가 필요해요.

예시 (Example)

//> using options -Wunused:all

def example(): Unit =
  val unused = 42
  println("hello")

에러 (Error)

-- [E198] Unused Symbol Warning: example.scala:4:6 -----------------------------
4 |  val unused = 42
  |      ^^^^^^
  |      unused local definition

해결 방법 (Solution)

사용하지 않는 심볼을 제거해요.

//> using options -Wunused:all

def example(): Unit =
  println("hello")

아니면 그 심볼을 사용해요.

//> using options -Wunused:all

def example(): Unit =
  val value = 42
  println(s"The value is $value")

또는 언더스코어(_) 식별자를 가진 val을 사용해서 경고를 억제할 수도 있어요.

//> using options -Wunused:all

def example(): Unit =
  val _ = 42
  println("hello")