Detect unused Scala code

Hi all,

is there any way (using some tool?) to detect unused code within a project? I’d like to find unused public methods, classes etc. to get rid of obsolete code.

Thanks for any pointers :slight_smile:

Hi. If you use IntelljIdea IDE, there is a default plugin that detect unused code. Maybe you can try with that tool.

Unfortunately that seems to only search for local symbols. I’d like to also see whether a class or a public method is not used anywhere in the project.

It’s a bit challenging, due to the nature of the JVM – you can only detect unused code within the context of a complete program, and the JVM doesn’t tend to think in those terms; it focuses more on individual classes. (In Scala.js, which actually assembles a full program, unused code just gets silently dropped at link time, I believe.)

It’s likely sort of impossible in principle due to reflection (in theory, any code could be invoked that way, and it would be hard for a tool to detect it); in practice it’s probably feasible for most programs, but it’s a pretty deep bit of analysis.

Don’t know if there is a fully-automated tool to deal with this. Personally, I manage it by making sure I have full test coverage of my primary entry points, and running scoverage regularly. Anything that isn’t covered by a test is automatically suspicious, and deserving of investigation…

1 Like

In IntelliJ (and, I guess, inany decent IDE), you can ask what are usages of a given type or method within the project. I suppose it should not be too hard to write something that does this for every type and method and notes which do not seem to be used anywhere.

I suppose the reason this is not more readily available is that such a search is costly and of limited value.

There is the problem that anything can be accessed via reflection.

Also, if I package your project up and use it in my project, I can access anything that is public.

It is not completely clear which are the entry points. For plain old Java apps its the main method (though you may have many of those, perhaps some unused), for other aps it may be different (e.g. applets and servlets).

If you method overrides another method, then there can be many calls to the base method and no way to figure out which ones call the overriding method.

Most often, your unused method is actually called, but the call itself sits in a branch that just never happens.

Most often, your unused code will consist of multiple interdependent types and methods.

For this reason, the best approach may be to skim over the codebase manually and wonder, what is still being used.

1 Like

Thanks for the responses!
I’m aware that there are problems with this approach (uses via reflection, by other projects and so on) but for my current use (a Play application) this is not really relevant. The existence of ‘Find Usages’ in IntelliJ (and similarly in other IDEs) is what actually frustrates me a bit: all the building blocks would be there, but the solution doesn’t seem to exist (I wouldn’t mind a long running time - it’s not something I’d use every day).
It seems for now I’m stuck with doing it manually (or with jducoeur’s approach using test coverage) or actually implementing it myself (maybe Metals would be a good starting point?)…

IntelliJ has that feature for Java code, so I doubt that it is a problem with Scala being on the JVM or the search being slow (IntelliJ creates a lot of indexing data on the code).

On the Intellij Scala plugin’s bug tracker, there wasn’t any mention of the feature, so probably none of the devs thought of implementing it yet. I’ve opened a feature request for porting the “Unused declaration” inspection provided for Java.