Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Wunused false positive in for-comprehension (2.13.15 regression) #21874

Closed
myazinn opened this issue Nov 1, 2024 · 2 comments
Closed

-Wunused false positive in for-comprehension (2.13.15 regression) #21874

myazinn opened this issue Nov 1, 2024 · 2 comments
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label

Comments

@myazinn
Copy link

myazinn commented Nov 1, 2024

Compiler version

2.13.15

Minimized code

object FalsePositiveUnused {

  def notOk1() =
    List(1).foldLeft(List(0)) { case (_, _) =>
      for {
        result <- List(1)
        usedVariable = result
      } yield usedVariable
    }

  def notOk2() =
    List(1).flatMap { case _ =>
      for {
        result <- List(1)
        usedVariable = result
      } yield usedVariable
    }

  def ok1() =
    for {
      result <- List(1)
      usedVariable = result
    } yield usedVariable

  def ok2() =
    List(1).flatMap { _ =>
      for {
        result <- List(1)
        usedVariable = result
      } yield usedVariable
    }

}

https://scastie.scala-lang.org/4Kbf4Z8VTHiwFJehlnp1jA

Output

[error] Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=unused-pat-vars, site=FalsePositiveUnused.notOk1.$anonfun.$anonfun.usedVariable
[error]         usedVariable = result
[error] Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=unused-pat-vars, site=FalsePositiveUnused.notOk2.$anonfun.$anonfun.usedVariable
[error]         usedVariable = result

Expectation

Code snippet should compile fine with "-Ywarn-unused", "-Xfatal-warnings".
The compiler complains that usedVariable is unused, but it definitely is. The code breaks only when for-comprehension is wrapped in partial function (at least from what I've found). Because List(1).flatMap { _ => works fine, while List(1).flatMap { case _ => does not 🤔

Same code works fine with 2.13.14
https://scastie.scala-lang.org/DVK5oQ3LQgGAFI7hzHjMrw
Scala 3 also works fine.

I guess the issue was introduced because of this ticket #18289 (thank you for that BTW, it's very appreciated despite this bug) . Maybe something went wrong during back porting to Scala 2.

@myazinn myazinn added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 1, 2024
@myazinn
Copy link
Author

myazinn commented Nov 1, 2024

ooops, wrong place, sorry

@myazinn myazinn closed this as completed Nov 1, 2024
@som-snytt
Copy link
Contributor

Duplicate of scala/bug#13041 which is fixed

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label
Projects
None yet
Development

No branches or pull requests

2 participants