I want to inspect my codebase to find both "Fruitless type test" warnings and "Comparing Unrelated types" warning Basically had a pretty big bug in our codebase which could have been avoided had we not ignored this warning. We want to now inspect the code to find if there are any other instances of this in our codebase?
I've checked all documented Scala compiler options and did not find any analogue to the IntelliJ Idea (I suppose you are using this IDE with Scala plugin) Inspection warning you posted. I guess this because ==
operation, which is then de-sugared to equals
method invokation is fine from compile stand point of view: boolean equals(Obejct obj)
- as you see from this method signature it is ok pass any object type, hence compiler does not complaining about that.
List of all Scala compiler options you can find here: https://docs.scala-lang.org/overviews/compiler-options/index.html
What you can do in this case
In short term perspective: Run inspection across all the project using Ctr+Alt+Shift+I
combination and type inspection name - for instance Comparing Unrelated types
(see for more details: https://www.jetbrains.com/help/idea/running-inspections.html)
In long term perspective: use Eq
type class from cats
library which fixes this issue: https://typelevel.org/cats/typeclasses/eq.html
Hope this helps!
Assuming you are using IntelliJ, try executing a single inspection
Analyse | Run Inspection by Name...
Comparing unrelated types
Inspection scope
to Whole project
Consider avoiding vanilla ==
in favour of ===
import cats.implicits._
1 == "" // res5: Boolean = false
1 === "" // compiler error
Consider wartremover
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.5")
wartremoverErrors ++= Warts.all
which gives something like
[wartremover:Equals] == is disabled - use === or equivalent instead
[error] val x = Some("") == Some(3)
Not specific to == bugs, but adding scalacOptions ++= Seq("-Xfatal-warnings")
to your build.sbt will convert warnings to errors and fail compilation. It's way safer to mark some code places as "ignore warnings" than ignore warnings by default.
You have various sbt plugins for code inspection that can be helpful too at compile time.
I do not advise plugging in extra libs (like cats) :
You want your runtime as simple as possible, and adding e.g import cats.implicits._
to your classes is pretty much the opposite of simple. For example, this import will happily create thousands of instances of various cats things at runtime, very possibly taking several seconds to instanciate them all since it will in fact inspect your whole codebase at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With