Tooling to find and remove unused derives?

Do we have any tooling to find and remove unused derived impls?

I've accumulated many unused derived impls in my project and they're causing problems in a few ways:

  • Adding new fields/variants can often cause issues because e.g. the type derives Hash but the new field's type doesn't.

    When this happens the first thing I try is removing the Hash derive. Often I'm able to remove it. It would be great if I could solve this problem automatically.

  • Because there's no direct link from a trait implementation to how use sites, when I look at a type and it derives e.g. Ord I often can't immediately tell why it implements it and whether it needs to implement it.

    If I refactor the code and stop using a derived trait implementation I don't get any warnings so the derive line stays, causing confusion later on.

A tool to remove all unused derives in a package would be really useful. Does something like this already exist?

(My project is not a library, but for libraries maybe it could only do it if it doesn't affect public API.)

4 Likes

I don't know of any tooling that already does this, it'd be helpful to see for types not in the public API.

While this would be different from a semver check, it may be interesting to see if the trustfall framework used for cargo-semver-checks can easily query this kind of thing.