Skip to content

false positive warning #23119

Open
Open
@mr-git

Description

@mr-git

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!

Metadata

Metadata

Assignees

Labels

area:lintingLinting warnings enabled with -W or -Xlintitype:bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions