Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: warn on unused trait impls #3214

Closed
RReverser opened this issue Sep 23, 2018 · 2 comments
Closed

Feature request: warn on unused trait impls #3214

RReverser opened this issue Sep 23, 2018 · 2 comments

Comments

@RReverser
Copy link

RReverser commented Sep 23, 2018

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.

@RReverser RReverser changed the title Feature request: warn on unused traits Feature request: warn on unused trait impls Sep 23, 2018
@RReverser
Copy link
Author

In fact, this upstream issue seems to partially cover this: rust-lang/rust#50927

@Manishearth
Copy link
Member

This requires a global analysis which is super hard to do from clippy.

This definitely would need to be done in the compiler, if they're willing to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants