Skip to content

Spurious warning in for expression #12018

@lambdista

Description

@lambdista

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions