Search code examples
scalasbtstatic-analysisunused-variablesscalameta

Scala tool to remove all unused code


I am writing a Scala plugin for an editor I use that would highlight all unused code paths (could be unused defs, vals, classes and implicits), and give the user an option to yank them out of the .scala file.

How can I do this? To simplify the problem, let's pretend we only have a single root level .scala file with no external dependency on libraries or any other code files.

Ideally I would want this to be an SBT plugin that, given a single such Foo.scala file, would spit out Foo_min.Scala file with all unused code removed.


Solution

  • Scalafix has a rewrite for this: RemoveUnusedImports

    Follow those instructions to run it: https://scalacenter.github.io/scalafix/#Installation