oleg-py/better-monadic-for

"parameter value .. is never used" with `if` in `for`

Daenyth opened this issue · 4 comments

This code:

val x =
 for {
    n <- Some(1)
    if n > 0
  } yield n

triggers the "unused parameter" lint:

Error:(99, 7) parameter value n in value $anonfun is never used
      n <- Some(1)

I can't reproduce that. Which version of bm4, Scala and combination of flags are you using?

scala 2.12.8, bm4 0.3.0, "io.github.davidgregory084" % "sbt-tpolecat" % "0.1.7" for flags

Seems -Ywarn-unused:params is just busted irrespective of plugin. E.g. it warns in this simple case:

object test extends App {
  val x =
    for {
      n <- Some(1)
      if n > 0
    } yield "Sup"
  println(x)
}

(scastie, but you might need to edit a code to trigger a recompile with full warnings), and if you do yield n, it's not reproducible on 0.3.1 with code you've provided.

@oleg-py, that expression has to be desugared into something like

Some(1).filter(n => n > 0).map(n => "Sup")

, which probably makes determining "whether n is used" difficult (since there is no single n). There are issues about this behavior, but they don't seem to going anywhere. And I see no good way to work around this in a plugin without changing semantics (by getting something more than filter and map to work with), or disabling this lint.

Looks like we'll just have to write it like this for the foreseeable future:

for {
  _ <- Some(1).filter(n => n > 0)
} yield "Sup"