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?
3 Answers
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!

- 4,043
- 1
- 11
- 28
Assuming you are using IntelliJ, try executing a single inspection
Analyse | Run Inspection by Name...
- Enter
Comparing unrelated types
- Set
Inspection scope
toWhole 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)

- 47,285
- 6
- 56
- 98
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.

- 8,355
- 6
- 29
- 47
-
1Well you can always just import what you need from cats. – Luis Miguel Mejía Suárez Mar 03 '20 at 11:59
-
That's definitely right (even if in fact not trivial)! But cats was just an example, my point is that there are solutions tailored for this problem that doesn't make your runtime heavier. – C4stor Mar 04 '20 at 08:26