WartRemover
The compiler already has a switch for a construct a team refuses, but the list of switches is short and each one is a flag of its own. WartRemover 3.6.3 is a compiler plugin that turns that idea into a library of warts: Var forbids mutable local state, Null forbids null, and a project enables the ones it agrees with. A forbidden construct is not a warning to discuss: the plugin makes it a compile error.
The plugin on the compiler command
A plugin is two flags: --compiler-plugin names the artifact, whose name carries the full Scala version, and -P:wartremover:traverser: names one wart. The program below is the counter of an earlier lesson, and with Var enabled it does not compile:
Counter.scala
@main def counter(): Unit =
var count = 0
for _ <- 1 to 3 do count += 1
println(count)
scala-cli run --server=false --compiler-plugin org.wartremover:wartremover_3.10.1:3.6.3 -P:wartremover:traverser:org.wartremover.warts.Var -P:wartremover:traverser:org.wartremover.warts.Null Counter.scala
2 | var count = 0
| ^^^^^^^^^^^^^
| [wartremover:Var] var is disabled
1 error found
Compilation failed
The quote starts at the source line because the header above it pads itself to the length of the path, which differs from machine to machine. The message is the rule's name and one sentence, and 1 error found is the compiler's own count. The same flags with Null enabled stop the next program, which assigns null on purpose:
Nullable.scala
@main def nullable(): Unit =
val name: String = null
println(name)
scala-cli run --server=false --compiler-plugin org.wartremover:wartremover_3.10.1:3.6.3 -P:wartremover:traverser:org.wartremover.warts.Var -P:wartremover:traverser:org.wartremover.warts.Null Nullable.scala
2 | val name: String = null
| ^^^^
| [wartremover:Null] null is disabled
1 error found
Compilation failed
Both failures are the point of the lesson: with the warts enabled, a value that may be missing is an Option, and a loop that counts is a fold or a range. The same command with a program that already follows the rules compiles and runs:
Clean.scala
@main def clean(): Unit =
val count = (1 to 3).size
println(count)
scala-cli run --server=false --compiler-plugin org.wartremover:wartremover_3.10.1:3.6.3 -P:wartremover:traverser:org.wartremover.warts.Var -P:wartremover:traverser:org.wartremover.warts.Null Clean.scala
3
A build writes the same two flags once, in scalacOptions, instead of on every command: the build.sbt lesson shows where a setting goes. A team enables the warts one at a time: Var and Null first, then the ones about exceptions and return, because a plugin that forbids everything at once turns a new project into a week of rewrites.
Related
- Scalafix: the same refusals as a separate command, with rules a project writes itself.
- Common errors: the compiler errors a beginner meets without a plugin.
- Option, Either and Try: what replaces null once the wart is on.
- Values and types: val and var, the pair the first wart is about.
Article author: Arthur Isaev