Reading Time: 2 minutes

Each time we write code, we are told to ‘maintain the coding standards’, yet many a time we fail to do so. Though there are tools which we use to help us in linting the code like scalastyle, scapegoat, scalafmt, scalariform etc. lack a bit, seems not enough to meet the standard. Basically though we use all these tools still there is wart and therefore there comes WartRemover to rescue us from these warts.

Now the question comes, What’s WartRemover ?

Well, WartRemover is a flexible Scala code linting tool. It takes the pain out of writing scala by removing some of the language’s nastier features. It works with the type system to improve the reliability of your code.

Different ways of using WartRemover

Using the sbt plugin

As a command-line tool

As a compiler plugin with manually provided scalac options

To derive macros

It catches the following issues:

Inference issues Type inferred as Any Type inferred as Nothing Type inferred as Product Type inferred as Serializable

Runtime type coercion asInstanceOf isInstanceOf use of any2stringadd

Partial methods and type-safety breaking features Use of List: head, tail, last Use of Option.get Use of LeftProjection.get Use of RightProjection.get Use of null

Patterns that damage reuse/reasoning Use of var Use of return Statements that return non-Unit values Functions taking default arguments



Oof… isn’t it a big list, however apparently it will truly rescue us from the warts and it is as simple as it sounds. Now let’s try this with an example. Open your sbt project and follow the steps given below:

1) Add the following to your project/plugins.sbt:

resolvers += Resolver.sonatypeRepo("releases") addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.11")

(NOTE: sbt-wartremover requires sbt version 0.13.5+)

2) Add the following to build.sbt:

addCompilerPlugin("org.brianmckenna" %% "wartremover" % "0.10") wartremoverWarnings ++= Warts.allBut(Wart.NoNeedForMonad) scalacOptions ++= Seq("-deprecation", "-Xlint")

3) Now write any code violating coding standards, say for example:

object ViolateNothing extends App{ val noCode = ??? val sampleList = List.empty }

4) Now run, sbt compile. It will show the warnings for the above scala warts like below –

So, this is just for one of the violations we do. There’s a long list of built-in warts it checks for. You can explore for that from this link.

Write more, worry less. Happy coding 🙂

References: