RemoveUnused
Removes unused imports and terms that reported by the compiler under -Ywarn-unused
See slick/slick/pulls#1736 for an
example diff from running sbt "scalafix RemoveUnused"
.
Installation
To use this rule:
- Enable the Scala compiler option
-Ywarn-unused
(or-Wunused
in 2.13). In sbt, this is done withscalacOptions += "-Ywarn-unused"
. - Disable
-Xfatal-warnings
if you have it enabled. This is required so the compiler warnings do not fail the build before running Scalafix. If you are running 2.12.13+ or 2.13.2+, you may keep-Xfatal-warnings
by modifying how specific warnings are handled viascalacOptions += "-Wconf:cat=unused:info"
. - This rule can't work yet on Scala 3 projects since the compiler option
warn-unused
is not yet available in Scala 3. You need to removeRemoveUnused
from.scalafix.conf
for Scala 3 projects.
Examples
Remove unused imports:
// before
import scala.List
import scala.collection.{immutable, mutable}
object Foo { immutable.Seq.empty[Int] }
// after
import scala.collection.immutable
object Foo { immutable.Seq.empty[Int] }
Remove unused local variables:
// before
def app = {
val unused = "message"
println("Hello world!")
}
// after
def app = {
println("Hello world!")
}
Remove unused private variables:
// before
object Main {
private def unused = "remove me"
def main() = println("Hello!")
}
// after
object Main {
def main() = println("Hello!")
}
Remove unused pattern match variables:
case class AB(a: Int, b: String)
// before
object Main {
val example = AB(42, "lol")
example match {
case AB(a, b) => println("Not used")
}
}
// after
object Main {
val example = AB(42, "lol")
example match {
case AB(_, _) => println("Not used")
}
}
Remove unused function parameters:
// before
object Main {
val f: String => Unit = unused => println("f")
}
// after
object Main {
val f: String => Unit = _ => println("f")
}
Formatting
This rule does a best-effort at preserving original formatting. In some cases, the rewritten code may be formatted weirdly
// before
import scala.concurrent.{
CancellationException, // A comment
TimeoutException
}
// after
import scala.concurrent. // A comment
TimeoutException
It's recommended to use a code formatter like Scalafmt after running this rule.
Configuration
Name | Type | Description |
---|---|---|
imports | Boolean | Remove unused imports |
privates | Boolean | Remove unused private members |
locals | Boolean | Remove unused local definitions |
patternvars | Boolean | Remove unused pattern match variables (compatible with Scala 2.12 and 2.13) |
params | Boolean | Remove unused function parameters (compatible with Scala 2.12 and 2.13) |
Defaults
RemoveUnused.imports = true
RemoveUnused.privates = true
RemoveUnused.locals = true
RemoveUnused.patternvars = true
RemoveUnused.params = true
-Ywarn-unused
Consult scala -Y
in the command-line for more information about using
-Ywarn-unused
.
$ scala -Ywarn-unused:help
Enable or disable specific `unused' warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
privates Warn if a private member is unused.
locals Warn if a local definition is unused.
explicits Warn if an explicit parameter is unused.
implicits Warn if an implicit parameter is unused.
params Enable -Ywarn-unused:explicits,implicits.
linted -Xlint:unused.
Default: All choices are enabled by default.