Over a million developers have joined DZone.

Remove Scala Warts With WartRemover

DZone's Guide to

Remove Scala Warts With WartRemover

WartRemover, a free, handy linting tool, will help ease the pain of Scala warts by pointing them out, making it easier to adhere to coding standards.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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 that we use to help us in linting code, like scalastyle, scapegoat, scalafmt, scalariform, etc., they seem to be lacking, and it never seems enough to meet the standard. Basically, though we use all these tools, there are still warts and, therefore, here comes WartRemover to rescue us.

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 our 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 that a big list. However, apparently it will truly rescue us from 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:

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+)

Add the following to build.sbt:

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

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

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

Now run, sbt compile. It will show the warnings for the above Scala warts like below:

Screenshot from 2017-06-15 11-21-50

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 that from this link.

Write more, worry less. Happy coding

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

java ,scala ,linting ,warts ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}