Skip to content

Feature request: warn on unused trait impls #3214

Closed
@RReverser

Description

@RReverser

Crate compilation often happens to be slow because of bunch of unnecessary trait implementations, especially via derives, that are not actually used in the public interface.

Example 1 (private trait on public type):

trait CustomTrait {
    fn some_method(&self);
}

pub struct S;

impl CustomTrait for S {
    fn some_method(&self) {}
}

pub fn f() -> S {
    S
}

You can see here that trait is not used anywhere in private code, not exposed to the public interface and is generally just a dead code, but, unlike regular unused methods, this currently compiles with no warning.

Example 2 (public trait on private type):

pub trait CustomTrait {
    fn some_method(&self);
}

struct S;

impl S {
    fn do_something_private(&self) {}
}

impl CustomTrait for S {
    fn some_method(&self) {}
}

pub fn f() {
    let s = S;
    s.do_something_private();
}

This time a type private to the library has a trait implementation that is, again, not used internally nor can be used outside because the type itself is not exposed.

I could put some more examples with #[derive(...)] but after macro expansion they would be analogous to the ones above, so probably not too interesting on their own (although do take time while proc macro does its job).

While this issue could potentially belong to Rust compiler itself, it seems worth trying to add this at least as a clippy warning first and see if it gains traction.

Also, apologies if that has been already discussed somewhere, the only related issue I found is Warn on unused/unnecessary trait bounds which is somewhat orthogonal to this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions