rust-lang / rust-lang/rust-clippy

Warn about trait bounds on struct and enum type parameters

Open
#1,689 26 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-unnecessary T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Traits are for behavior.

impl<T> Data<T> where T: Behavior { /* behavior, okay */ }

fn behave<T>() where T: Behavior { /* also behavior, also okay */ }

struct Data<T> where T: Behavior { /* DON'T DO THIS */ }

The only exception is things like Cow that use associated types to define data.

My current understanding is that any data structure not using associated types should not have trait bounds (?Sized doesn't count). Redundant bounds like this suck because they transitively infect anything that names the type in any way. Consider this enum.

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
enum Compact<T: CompactPart, H: Serialize + Deserialize + Clone + 'static> {
    Decrypted {
        header: Header<H>,
        payload: T,
    },
    Encrypted(Encrypted),
}

// kill me
fn print_the_thing<T, H>(c: Compact<T, H>)
    where T: CompactPart + Debug,
          H: Serialize + Deserialize + Clone + 'static + Debug
{
    println!("{:?}", c);
}

Without trait bounds on the data structure:

fn print_the_thing<T: Debug, H: Debug>(c: Compact<T, H>) {
    println!("{:?}", c);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the Rust examples in the issue, including the linked src/jwe.rs example, and determine the intended cases for a warning on trait bounds attached to struct and enum type parameters. The payload names no Clippy implementation entry point or test file, so the completion criteria are the requested warning behavior and coverage for the shown valid and invalid patterns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.