Mix unused - simple task for looking for unused public functions

After request in this thread I created simple task for looking for unused public functions (mix xref unreachable returns information only about unused private functions).

Code: https://github.com/hauleth/mix_unused

Currently it isn’t released on Hex as I am still polishing the edges. Feel free to test this out.

14 Likes

Thanks! I was just checking out xref to see if it already did this. I’ll check it out this weekend. I was hoping to hook this into a CI pipeline to remind devs to trim dead code

1 Like

So I have finally find some time to make it work for Elixir 1.10 and publish it on Hex.

Now it is implemented via compiler tracing, so it is only Elixir 1.10 and later, but thanks to that, it also works with ElixirLS.

Beware that it is still WIP, and there are some features missing, and it will print a lot (and by a lot I mean A LOT) of false positives as it do not filter out generated functions nor Elixir macros. So prepare for filtering out quite a lot.

Testers, suggestions, and pull requests are welcome.

What is not currently working:

  • Macros filtering
  • Generated functions filtering (so for example you need to filter out {:_, :__schema__, :_} if you use Ecto, all controller actions if you use Phoenix, etc.)
  • Lines for given functions, so all missing functions will be reported on top of the file
  • No way to filter out functions using regular expression
  • No way to filter out functions using range arity
12 Likes

Release v0.2.0

Added few useful features:

  • macros are supported now
  • you can filter out functions and modules using regular expressions
  • you can filter arity using ranges
  • lines are properly supported, which mean that ElixirLS will report unused functions as a lens in correct places
6 Likes

Forgot to post it there:

v0.3.0 released

Most important change is check whether structures are used and will inform if not. Just pattern matching on the structure is enough to mark it as used.

From minor changes - it now do not fail on recompilation via Phoenix LiveReload or call to recompile/0 from IEx shell.

6 Likes

v0.4.0 released

Bug Fixes

  • do not fail if cannot write manifest (5f407c1)

Documentation

  • add documentation about using predicate filters (1360fd5)
  • misc doc changes (05c96f5)

Features

  • extract functions metadata to separate file (4cf981d)
  • add possibility to filter using metadata of the entry (9fa8838)
  • add support for unary functions in filters (684007e)
  • scan for transitive unused functions (80825bd)
  • add sponsoring link to the Hex package links (291c914)
  • add analyzer to detect functions called only recursively (48411d1)
  • add covertool support (cf476e8)
  • cleanup analyzers (5b79d32)
  • add analyzer for functions that could be private (c0bf50d)
5 Likes

v0.4.1 released

The main reason for that is that older version was reporting false positives (used function reported as unused) if there was a call cycle.

Bug Fixes

  • resolve issue with unused analyzer reporting false positives (3b86630)

Documentation

  • Improve Usage Docs a bit around usage (a191c46)
9 Likes