Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 979
klogd Avatar asked Mar 29 '16 12:03

klogd


2 Answers

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))
like image 188
hasumedic Avatar answered Oct 10 '22 11:10

hasumedic


With your values, t.contains(true).&&(f.contains(false)) always returns true. So you could simplify it by simply writing true, i.e by just executing the print without an if condition.

like image 43
John K Avatar answered Oct 10 '22 10:10

John K