Scalafix

Scalafix

  • User guide
  • Developer guide
  • Browse sources
  • GitHub

›Rules

Usage

  • Installation
  • Configuration
  • Suppressing rules

Rules

  • Built-in rules
  • DisableSyntax
  • ExplicitResultTypes
  • LeakingImplicitClassVal
  • NoAutoTupling
  • NoValInForComprehension
  • ProcedureSyntax
  • RedundantSyntax
  • RemoveUnused
  • Using external rules
  • Community rules

Misc

  • Related projects
Edit

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 with scalacOptions += "-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.13.2 or later, you may keep -Xfatal-warnings by modifying how specific warnings are handled via scalacOptions += "-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 remove RemoveUnused 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

NameTypeDescription
importsBooleanRemove unused imports
privatesBooleanRemove unused private members
localsBooleanRemove unused local definitions
patternvarsBooleanRemove unused pattern match variables (compatible with Scala 2.12 and 2.13)
paramsBooleanRemove 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.
← RedundantSyntaxUsing external rules →
  • Installation
  • Examples
  • Formatting
  • Configuration
    • Defaults
  • -Ywarn-unused
Scalafix
Docs
Get startedRulesExtend Scalafix
Community
Chat on DiscordDiscuss on Scala Users
More
GitHub
Copyright © 2023 Scala Center