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

new lint: Unused trait bound(s)

Open taufik-rama opened this issue 3 years ago • 0 comments

What it does

For any trait bounds that's not used on the function body, create an "unused" warning

Lint Name

unused_trait_bound

Category

pedantic

Advantage

  • More "tight" code, we can be sure that each bounds will be used
    • and maybe in other words: more cleaner code since unused bound can be safely deleted

Drawbacks

  • Might not be suitable for library APIs, since removing bounds (even if it's unused) might be a breaking change

Example

trait Database {
    fn database(&self);
}

trait Redis {
    fn redis(&self);
}

fn get_user<C>(conn: C)
where
    C: Database + Redis, // this `Redis` bound should be warned
{
    // we just so happen to forgot to check for Redis cache
    conn.database()
}

Could be written as:

trait Database {
    fn database(&self);
}

trait Redis {
    fn redis(&self);
}

fn get_user<C>(conn: C)
where
    C: Database, // either remove the bound, or
{
    // use the bound
    // conn.redis()
    conn.database()
}

taufik-rama avatar Aug 10 '22 01:08 taufik-rama