Add the following to your project/plugins.sbt:

addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.2.0")

NOTE: sbt-wartremover requires sbt version 0.13.5+.

Now, you can proceed to configure the linter in your build.sbt. By default, all errors and warnings are turned off. To turn on all checks that are currently considered stable, use:

wartremoverErrors ++= Warts.unsafe

To turn on all available errors (some have false positives), use:

wartremoverErrors ++= Warts.all

Similarly, to just issue warnings instead of errors for all built-in warts, you can use:

wartremoverWarnings ++= Warts.all    // or Warts.unsafe

You can also use scopes, e.g. to turn on all warts only for compilation (and not for the tests nor the sbt console), use:

wartremoverErrors in (Compile, compile) ++= Warts.all

To choose warts more selectively, use any of the following:

wartremoverErrors ++= Warts.allBut(Wart.Any, Wart.Nothing, Wart.Serializable)

wartremoverWarnings += Wart.Nothing

wartremoverWarnings ++= Seq(Wart.Any, Wart.Serializable)

Suppressing Errors & Warnings

To exclude a specific piece of code from one or more checks, use the SuppressWarnings annotation:

@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null"))
var foo = null

To exclude a file from all checks, use wartremoverExcluded in your build.sbt file:

wartremoverExcluded += baseDirectory.value / "src" / "main" / "scala" / "SomeFile.scala"

Other ways of using WartRemover

Apart from using the sbt plugin to set it up for your project, WartRemover can also be used in the following ways:

  • as a command-line tool,
  • as a compiler plugin with manually provided scalac options,
  • to derive macros.

Command-line

Compile the command-line tool via sbt core/assembly and then use the provided wartremover shell script:

$ sbt core/assembly

  ...

$ ./wartremover -traverser org.wartremover.warts.Unsafe core/src/main/scala/wartremover/Plugin.scala
core/src/main/scala/wartremover/Plugin.scala:15: error: var is disabled
  private[this] var traversers: List[WartTraverser] = List.empty
                    ^

Compiler plugin (manually)

Add the following to build.sbt:

resolvers += Resolver.sonatypeRepo("releases")

addCompilerPlugin("org.wartremover" %% "wartremover" % "2.2.0")

scalacOptions += "-P:wartremover:traverser:org.wartremover.warts.Unsafe"

By default, WartRemover generates compile-time errors. If you want to be warned only, use an only-warn-traverser:

scalacOptions += "-P:wartremover:only-warn-traverser:org.wartremover.warts.Unsafe"

If you don’t want to perform the checks in some file, you can use:

scalacOptions += "-P:wartremover:excluded:ABSOLUTE_PATH_TO_THE_FILE"

The excluded option accepts a colon-separated list of absolute paths to files to ignore.

To use your custom WartTraverser, you’ll need to provide a classpath where it can be found:

scalacOptions += "-P:wartremover:cp:someUrl"

Apache Maven

You can use WartRemover in Maven by employing it as a compilerPlugin to scala-maven-plugin:

<build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <compilerPlugins>
                        <compilerPlugin>
                            <groupId>org.wartremover</groupId>
                            <artifactId>wartremover_2.11</artifactId>
                            <version>2.2.0</version>
                        </compilerPlugin>
                    </compilerPlugins>
                    <args>
                        <arg>-P:wartremover:only-warn-traverser:org.wartremover.warts.Unsafe</arg>
                    </args>
                </configuration>
            </plugin>
        </plugins>
    </build>  

See the notes on the compiler plugin above for options to pass as <arg>s.

Macros

You can make any wart into a macro, like so:

scala> import language.experimental.macros
import language.experimental.macros

scala> import org.wartremover.warts.Unsafe
import org.wartremover.warts.Unsafe

scala> def safe(expr: Any) = macro Unsafe.asMacro
safe: (expr: Any)Any

scala> safe { null }
<console>:10: error: null is disabled
              safe { null }
                     ^