Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 240
pathikrit Avatar asked Mar 09 '16 22:03

pathikrit


People also ask

How do I delete unused variables in IntelliJ?

In IntelliJ, in a . java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O.

How do I find unused codes in IntelliJ?

java file the unused code is ordinarily grayed out or features a green underline saying this code will likely (likely since of a few peculiar JNI/Reflection corner cases) be unused.


2 Answers

Scalafix has a rewrite for this: RemoveUnusedImports

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

like image 128
Guillaume Massé Avatar answered Oct 18 '22 20:10

Guillaume Massé


You need some kind of semantic API to traverse over the code and ask questions like "is this variable/import ever used" ?

As far as I know, Intellij uses Meta programming System to achieve same goals. For scala, you can wait for scalameta's 2.0 release, which (most probably) will support semantic API.

like image 4
dveim Avatar answered Oct 18 '22 22:10

dveim