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

Lint to check unnecessary trait bounds

Open laralove143 opened this issue 2 years ago • 1 comments

What it does

Some trait bounds aren't required, though they may be added for semantic purposes, this is rare. An example is say we have this function:

async fn<T: Send + Sync>foo() { ... }

then it no longer needs async so we change it to

fn<T: Send + Sync>foo() { ... }

but we forgot to remove the Send + Sync bounds and they slip unnoticed, this is bad because users can no longer use types that don't implement Send + Sync even though they actually can

Lint Name

unnecessary_trait_bounds

Category

correctness

Advantage

  • Removes unnecessary restrictions
  • Makes code more clear
  • Possibly even frees a dependency if the trait is from a crate and it's not used otherwsie

Drawbacks

Basically warns on almost every trait bound in struct definitions:

struct<T: Send> Foo<T> { ... }

Whether this is good design is controversial, this could be separated to another lint but I don't see it necessary as users can simply allow it

Example

fn print<T: Debug + Display>(x: T) {
  println!("{x:?}");
}

Could be written as:

fn print<T: Debug>(x: T) {
  println!("{x:?}");
}

laralove143 avatar May 25 '23 20:05 laralove143