Open
Description
Compiler version
3.7.0
Minimized example
@main
def make: IndexedSeq[FalsePositive] =
for {
i <- 1 to 2
given Int = i
fp = FalsePositive()
} yield fp
class FalsePositive(using Int):
def usage(): Unit =
println(summon[Int])
Output Error/Warning message
[warn] -- [E198] Unused Symbol Warning: /<path>/main/src/main/scala/FalsePositive.scala:5:4
[warn] 5 | given Int = i
[warn] | ^^^^^^^^^
[warn] | unused pattern variable
[warn] one warning found
Why this Error/Warning was not helpful
The message was unhelpful because the given Int
is actually used - this is a "false positive".
Suggested improvement
The issue is with how code is structured in for-comprehension. If for-comprehension gets restructured to:
for {
i <- 1 to 2
given Int = i
} yield FalsePositive() // <=== notice that initialization happens without intermediate variable
The warning is not triggered any more!