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

Spurious warning in for expression #12018

Closed
lambdista opened this issue May 28, 2020 · 2 comments
Closed

Spurious warning in for expression #12018

lambdista opened this issue May 28, 2020 · 2 comments

Comments

@lambdista
Copy link

reproduction steps

using Scala 2.13.2,

$ scala -Wunused
Welcome to Scala 2.13.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_252).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

val optA: Option[Int] = Option(42)
val optB: Option[Int] = Option(0)

val x1: Option[Int] = for {
  a <- optA
  b <- optB
  if a != b
} yield a


// Exiting paste mode, now interpreting.


  b <- optB
  ^
<pastie>:6: warning: parameter value b in anonymous function is never used
val optA: Option[Int] = Some(42)
val optB: Option[Int] = Some(0)
val x1: Option[Int] = Some(42)

problem

It gives the warning because b is not used in yield. Still, it is used in if a != b. I guess that for comprehension is desugared more or less in:

optA.flatMap { a =>
  optB.withFilter(b => a != b).map(b => a)
}

Instead it should be desugared in the following one in order not to get the warning:

optA.flatMap { a =>
  optB.withFilter(b => a != b).map(_ => a)
}
@som-snytt
Copy link

The umbrella issue #10287

Similar to #11175 (different part of the desugared expression) and #11400 (in the map but refutable pattern).

Probably it's time to consolidate at #10287

@SethTisue
Copy link
Member

SethTisue commented May 29, 2020

agreed, duplicate (or close enough) of #10287, feel free to add the code in question as a comment there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants