rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Check that the return type and parameters of a trait method without a body are `Sized`

Open y21 opened this issue 1 year ago • 0 comments

What it does

rustc only checks that parameters and the return type are Sized if the function has a body. That means that one can write a useless trait that has an impossible-to-implement method, without any errors in the trait definition:

trait Foo {
  fn bar(&self, _: str);
}

Trying to implement this trait (method) however will lead to the error that we'd expect.

It might be good to have a lint for this that warns(/errors?) at the method declaration site inside of the trait.

(Technically also applies to extern blocks, but probably not worth it, since they are not FFI safe and the compiler already warns that)

Advantage

This kind of mistake could go unnoticed if e.g. a crate declares/exports such a trait but never tried implementing it.

Drawback

This will become obsolete when/if the unsized_fn_params feature (ever) gets stabilized. The lint also needs to make sure that that feature is not enabled.

It might also not really be all that useful in practice because the trait method is also impossible to call. So even if a crate creates such a trait and never tries to implement it, the author will most likely notice the mistake once they try using it in some way.

y21 avatar Apr 16 '24 16:04 y21