rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
10.89k stars 1.46k forks source link

`unimplemented_default_member` #12727

Closed mladedav closed 2 weeks ago

mladedav commented 2 weeks ago

What it does

Forces useers to implement all methods in a trait, including all methods with default implementations.

This would be allow-by-default restriction lint. This makes sense to turn on only in specific scenarios.

Advantage

Drawbacks

Example

trait Record {
  fn record_debug(field: &dyn Debug);

  fn record_u64(field: u64) {
    record_debug(field);
  }

  fn record_i64(field: i64) {
    record_debug(field);
  }
}
trait HasRecord {
    type MyRecord: Record;
    fn my_record() -> Self::MyRecord;
}

impl Record for T where T: HasRecord {
  fn record_debug(field: &dyn Debug) {
     Self::my_record().record_debug();
  }

  fn record_u64(field: u64) {
     Self::my_record().record_u64()
  }

  fn record_i64(field: i64) {
     Self::my_record().record_i64()
  }
}

Now if the Record trait is extended with a new method with default implementation such as record_str(str: &str) the default implementation would be bad for implementations leveraging the HasRecord type. As I user, I'd like a way to tell clippy that I want to be notified, if any method is not explicitly defined in a given impl block.

Alexendoo commented 2 weeks ago

Good news, we have a lint for this: https://rust-lang.github.io/rust-clippy/master/index.html#/missing_trait_methods

mladedav commented 2 weeks ago

Thanks and sorry for the noise. I did try to find it (unimplemented, trait and keywords like that) but didn't find it.