Last Updated: February 25, 2016
· sksamuel

Scala static code analysis (linter)

One of the popular things on a Java project is to integrate findbugs or checkstyles to generate compile time warnings for many common pitfalls. You can do the same in Scala with the use of the scapegoat project. https://github.com/sksamuel/scalac-scapegoat-plugin

Integrate with your build by adding the sbt-plugin to your build and then, at compile time, code which triggers one of the so called inspections will be flagged in the console.

A sample output would be something like:

[warning] [scapegoat] Unused method parameter - org.sksamuel.util.ClassIterator.scala:46
[warning] [scapegoat] Slow list append - org.sksamuel.util.ClassIterator.scala:137
[warning] [scapegoat] Use of var - org.sksamuel.util.ClassIterator.scala:22
[warning] [scapegoat] Use of var - org.sksamuel.util.Iterator.scala:157
[   info] [scapegoat]: Inspecting compilation unit [FileUtil.scala]
[warning] [scapegoat] Empty if statement - org.sksamuel.util.FileUtil.scala:157
[warning] [scapegoat] Type shadowing - org.sksamuel.util.FileUtil.scala:180

The kinds of things flagged by the tool include non-idiomatic code (eg parameterless method returns unit), unsafe method calls (eg Option.get or asInstanceOf), unnecessary code (eg .toString on a String), and general improvements (eg sealed trait has no implementations, var could be val).