Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::type_repetition_in_bounds: type `T` has already been used as a bound predicate
What it meansThis lint warns about unnecessary type repetitions in trait bounds It is in the nursery group and off by default.
What you see
warning: type `T` has already been used as a bound predicate --> main.rs:3:36 | 3 | pub fn foo<T>(t: T) where T: Copy, T: Clone {} | ^^^^^^^^ | = help: consider combining the bounds: `T: Copy + Clone` = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#type_repetition_in_bounds = note: `-W clippy::type-repetition-in-bounds` implied by `-W clippy::nursery`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Repeating the type for every bound makes the code less readable than combining the bounds
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
pub fn foo<T>(t: T) where T: Copy, T: Clone {}
Write this instead
pub fn foo<T>(t: T) where T: Copy + Clone {}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
type_repetition_in_bounds = "warn"In code
#![warn(clippy::type_repetition_in_bounds)]One run
cargo clippy -- -W clippy::type-repetition-in-boundsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
max-trait-bounds: The maximum number of bounds a trait can have to be lintedmsrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
This lint warns about unnecessary type repetitions in trait bounds
Why is this bad?
Repeating the type for every bound makes the code less readable than combining the bounds
Example
pub fn foo<T>(t: T) where T: Copy, T: Clone {}
Use instead:
pub fn foo<T>(t: T) where T: Copy + Clone {}
Configuration
-
max-trait-bounds: The maximum number of bounds a trait can have to be linted(default:
3) -
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- transmute_undefined_reprtransmute from `main::Foo<u32>` to `main::Foo<i32>`, both of which have an undefined layout
- trivial_regexChecks for trivial regex creation (with Regex::new, RegexBuilder::new, or RegexSet::new)
- tuple_array_conversionsit looks like you're trying to convert a tuple to an array
- uninhabited_referencesdereferencing a reference to an uninhabited type would be undefined behavior
- unnecessary_struct_initializationunnecessary struct building
- unused_peekable`peek` never called on `Peekable` iterator
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?