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

Add a lint for unintentional usage of default trait methods #9661

Closed
shepmaster opened this issue Oct 16, 2022 · 1 comment · Fixed by #9670
Closed

Add a lint for unintentional usage of default trait methods #9661

shepmaster opened this issue Oct 16, 2022 · 1 comment · Fixed by #9670
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@shepmaster
Copy link
Member

What it does

Fires when a trait implementation relies on a default method. This is primarily useful for "delegating" trait implementations that want to do a bit of work in addition to a lower implementation

Lint Name

unintentional_default_trait_methods

Category

restriction

Advantage

If a new default trait is added, it will be more obvious that we need to update our code to account for that method.

Drawbacks

There may be new default methods that we do want to rely on. I don't actually know if it's possible, but if the lint could be parameterized to ignore specific default methods, that would be nice.

Example

trait ExampleTrait {
    fn alpha(&mut self) -> bool {
        false
    }
    
    fn omega(&mut self) -> bool {
        false
    }
}

struct OuterImplementation<T> {
    inner: T,
    count: usize,
}

// This is bad; we don't know what methods `T` implements.
// If a new default function is added to `ExampleTrait`, we
// won't be doing our additional work.
impl<T: ExampleTrait> ExampleTrait for OuterImplementation<T> {
    fn alpha(&mut self) -> bool {
        self.count += 1;
        self.inner.alpha()
    }
}

If #[deny(unintentional_default_trait_methods)] was added to the impl ExampleTrait for OuterImplementation line, then an error like

This implementation of `ExampleTrait` uses the default trait method `omega`, but it should be deliberately implemented

There could even be a fix-it that adds the method stub.

@shepmaster shepmaster added the A-lint Area: New lints label Oct 16, 2022
@Alexendoo
Copy link
Member

This is a cool idea, I've wanted that before but didn't think of having it be a lint

I'll take a crack at it

@Alexendoo Alexendoo self-assigned this Oct 16, 2022
@flip1995 flip1995 added the good-first-issue These issues are a good way to get started with Clippy label Oct 17, 2022
@bors bors closed this as completed in 4612fdf Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants