View Source mix compile.unused (mix_unused v0.4.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 additional 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).

To ignore warnings about unused structs you need to use "special" syntax in form of {StructModule, :__struct__, 0}.

The pattern list can also take the predicate function which can be either unary or binary function. First argument will be mfa/0 and second argument (in case of the binary function) will be MixUnused.Meta.t/0.

Documentation metadata

Functions that have export: true in their metadata will be automatically treated as exports for usage by external parties and will not be marked as unused.

Options

  • --severity - severity of the reported messages, defaults to hint. Other allowed levels are information, warning, and error.
  • --warnings-as-errors - if the severity is set to :warning and there is any report, then fail compilation with exit code 1.