Scalastyle Bulan 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 I don’t really see how this simplifies it, can anyone explain what is happening?

Update It does not appear to be related to whether the shafts are known at compile time or locally defined. The following code also receives a warning that the expression can be simplified:

 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 understand how I should make this simpler, is there some strange [Boolean] option comparing me missing?

+5
source share
2 answers

It seems that you agree with the use of the method call. Or everything in the form of infix:

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

Or all in the form of a regular method:

 t.contains(true).&&(f.contains(false)) 
+2
source

With your values, t.contains(true).&&(f.contains(false)) always returns true . Therefore, you can simplify it by simply writing true , i.e. Just doing print without an if condition.

+1
source

Source: https://habr.com/ru/post/1245982/


All Articles