mix compile.unused (mix_unused v0.2.1)

Compile project and find uncalled public functions.

Warning

This isn't perfect solution and this will not find dynamic calls in form of:

apply(mod, func, args)

So this mean that, for example, if you have custom child_spec/1 definition then this will return such function as unused even when you are using that indirectly in your supervisor.

Configuration

You can define used functions by adding pattern in unused: [ignored: [⋯]] in your project configuration:

def project do
  [
    # ⋯
    unused: [
      ignore: [
        {MyApp.Foo, :child_spec, 1}
      ]
    ],
    # ⋯
  ]
end

Patterns

unused patterns are similar to the match specs from Erlang, but extends their API to be much more flexible. Simplest possible patter is to match exactly one function, which mean that we use 3-ary tuple with module, function name, and arity as respective elements, ex.:

[{Foo, :bar, 1}]

This will match function Foo.bar/1, however often we want to use more broad patterns, in such case there are few tricks we can use. First is to use :_ which will mean "wildcard" aka any value will match, ex.:

[{:_, :child_spec, 1}]

Will ignore all functions child_spec/1 in your application (you probably should add it, as unused is not able to notice that this function is used even if it is used in any supervisor, as it will be dynamic call).

In additiona to wildcard matches, which isn't often what we really want, we can use regular expressions for module and function name or range for arity:

[
  {:_, ~r/^__.+__\??$/, :_},
  {~r/^MyAppWeb\..*Controller/, :_, 2},
  {MyApp.Test, :foo, 1..2}
]

To make the ignore specification list less verbose there is also option to omit last :_, i.e.: {Foo, :bar, :_} is the same as {Foo, :bar}, if you want to ignore whole module, then you can just use Foo (it also works for regular expressions).

Options

  • severity - severity of the reported messages, defaults to hint. Other allowed levels are information, warning, and error.