better-monadic-for icon indicating copy to clipboard operation
better-monadic-for copied to clipboard

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

Open Daenyth opened this issue 6 years ago • 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)

Daenyth avatar Jul 18 '19 20:07 Daenyth

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

oleg-py avatar Jul 19 '19 07:07 oleg-py

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

Daenyth avatar Jul 19 '19 19:07 Daenyth

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 avatar Oct 11 '19 09:10 oleg-py

@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"

nigredo-tori avatar Oct 11 '19 12:10 nigredo-tori