wartremover - a flexible Scala code linting tool

Wartremover is a simple scala compiler plugin that lets you run linting checks against your source files. It's configured to run after the typer phase, so the AST will contain all the necessary type information by this point. The linting rules are expressed as Traverser-s. The plugin simply walks through these Traverser-s, finally setting the output (error and warning) on the given CompilationUnit if needed.

Wartremover comes with 12-14 linting checkers by default but it's very easy to implement your own versions. In this post, we will attempt to just do that.

Before we get down to business

The only thing we need to be familiar with is the new reflection API. I would highly recommend to read about Mirrors and Trees first.

TL;DR

- a Universe —which can be compile or runtime — gives access to main concepts such as Trees

- all information provided by the reflection API can be obtained via various mirrors (for example: Classloader and Invoker i.e. reflective mirrors)

- A Mirror provides three generic ways to create a generic Symbol reference: staticPackage, staticModule and staticClass

- A TypeSymbol represents type, class, and trait declarations

- A TermSymbol represents val, var, def, and object declarations as well as packages and value parameters

While this short summary is far from comprehensive, it should be enough to get started!

Disable JavaConversions

1. motivation: scala.collection.JavaConversions is arguably doing the wrong thing when "massaging" types via implicits, so let's prevent its usage and recommend using the more explicit scala.collection.JavaConverters instead.

Disable scala.collection.mutable._

2. motivation: a team might choose to allow using only persistent scala collections in their codebase and fall back to java.util data structures if mutation was really necessary (similar to what clojure does). So let's disable scala's mutable collections.

(You can find the whole changeset here)

Finally, If you want to learn more, I would suggest to read through the implementation of the built-in checkers.

Happy linting!