Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I inspect my scala codebase to find all warnings of type: "Comparing Unrelated types"?

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?

like image 571
Rahil Shah Avatar asked Mar 03 '20 07:03

Rahil Shah


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!

like image 147
Ivan Kurchenko Avatar answered Oct 25 '22 11:10

Ivan Kurchenko


Assuming you are using IntelliJ, try executing a single inspection

  1. Analyse | Run Inspection by Name...
  2. Enter Comparing unrelated types
  3. Set 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)
like image 41
Mario Galic Avatar answered Oct 25 '22 09:10

Mario Galic


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.

like image 1
C4stor Avatar answered Oct 25 '22 10:10

C4stor