E176: Unused Non-Unit Value — 사용되지 않는 Non-Unit 값
E176: Unused Non-Unit Value — 사용되지 않는 Non-Unit 값
구문 위치에서 Unit이 아닌 값이 계산됐지만 결코 사용되지 않을 때 발생하는 경고예요.
본문
이 경고는 어떤 표현식의 결과가 조용히 버려지는 상황을 찾아내요. 계산한 값을 사용하거나 할당하는 것을 잊은 프로그래밍 실수를 나타낼 수 있어요.
이 경고는 -Wnonunit-statement 컴파일러 플래그가 켜져 있을 때만 나와요.
예시 (Example)
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
list.map(_ + 1) // result is unused
println("done")
에러 (Error)
-- [E176] Potential Issue Warning: example.scala:5:10 --------------------------
5 | list.map(_ + 1) // result is unused
| ^^^^^^^^^^^^^^^
| unused value of type List[Int]
해결 방법 (Solution)
결과가 의도된 것이라면, 명시적으로 버리거나 사용해주세요:
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
val _ = list.map(_ + 1) // explicitly discard
println("done")
대신 결과를 사용해도 돼요:
//> using options -Wnonunit-statement
def example(): Unit =
val list = List(1, 2, 3)
val transformed = list.map(_ + 1)
println(s"Transformed: $transformed")
더 알아보기
결과가 불필요하면 val _ =로 버리고, 필요하면 변수에 담아 사용하는 게 깔끔해요.