scalascala-collectionsscalastyle

Scalastyle Boolean expression can be simplified


Scalastyle (intellij 2016.1 defaults) says this boolean expression can be simplified

val t = Option(true)
val f = Option(false)
if(t.contains(true) && f.contains(false)) {
  println("booop")
}

I can get rid of this by changing the if to:

if(t.contains(true).&&(f.contains(false)))

Or by changing && to &

But not really seeing how this is simplifying it, could anyone explain what's going on?

Update It doesn't appear to be related to if the vals are known at compile time, or them being locally defined. The following code also get's the warning that the expression can be simplfied:

object TestFoo {
  def bar(t: Option[Boolean]) = {
    val f = Option(scala.util.Random.nextBoolean)
    if (t.contains(true) && f.contains(false)) println("booop")
  }
  def main(args: Array[String]) = bar(t = Option(scala.util.Random.nextBoolean))
}

I just don't get how I'm supposed to make that any simpler, is there some strange Option[Boolean] comparing I'm missing out on?


Solution

  • It seems to suggest you being consistent with the method call usage. Either everything in infix form:

    (t contains true) && (f contains false)
    

    Or everything in regular method call form:

    t.contains(true).&&(f.contains(false))