Skip to content

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

@shepmaster

Description

@shepmaster

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.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions