Skip to content

Commit 754bfb1

Browse files
committed
Add configurable threshold for type_repetition_in_bounds lint
1 parent c493090 commit 754bfb1

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

clippy_lints/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
996996
store.register_late_pass(|| box checked_conversions::CheckedConversions);
997997
store.register_late_pass(|| box integer_division::IntegerDivision);
998998
store.register_late_pass(|| box inherent_to_string::InherentToString);
999-
store.register_late_pass(|| box trait_bounds::TraitBounds);
999+
let max_trait_bounds = conf.max_trait_bounds;
1000+
store.register_late_pass(move || box trait_bounds::TraitBounds::new(max_trait_bounds));
10001001
store.register_late_pass(|| box comparison_chain::ComparisonChain);
10011002
store.register_late_pass(|| box mut_key::MutableKeyType);
10021003
store.register_late_pass(|| box modulo_arithmetic::ModuloArithmetic);
@@ -1033,7 +1034,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10331034
let array_size_threshold = conf.array_size_threshold;
10341035
store.register_late_pass(move || box large_stack_arrays::LargeStackArrays::new(array_size_threshold));
10351036
store.register_late_pass(move || box large_const_arrays::LargeConstArrays::new(array_size_threshold));
1036-
store.register_late_pass(move || box floating_point_arithmetic::FloatingPointArithmetic);
1037+
store.register_late_pass(|| box floating_point_arithmetic::FloatingPointArithmetic);
10371038
store.register_early_pass(|| box as_conversions::AsConversions);
10381039
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
10391040
store.register_late_pass(|| box let_underscore::LetUnderscore);

clippy_lints/src/trait_bounds.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ use rustc_hir::{GenericBound, Generics, WherePredicate};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
77

8-
#[derive(Copy, Clone)]
9-
pub struct TraitBounds;
10-
118
declare_clippy_lint! {
129
/// **What it does:** This lint warns about unnecessary type repetitions in trait bounds
1310
///
@@ -29,6 +26,18 @@ declare_clippy_lint! {
2926
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
3027
}
3128

29+
#[derive(Copy, Clone)]
30+
pub struct TraitBounds {
31+
max_trait_bounds: u64,
32+
}
33+
34+
impl TraitBounds {
35+
#[must_use]
36+
pub fn new(max_trait_bounds: u64) -> Self {
37+
Self { max_trait_bounds }
38+
}
39+
}
40+
3241
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]);
3342

3443
impl<'tcx> LateLintPass<'tcx> for TraitBounds {
@@ -45,6 +54,9 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
4554
let mut applicability = Applicability::MaybeIncorrect;
4655
for bound in gen.where_clause.predicates {
4756
if let WherePredicate::BoundPredicate(ref p) = bound {
57+
if p.bounds.len() as u64 > self.max_trait_bounds {
58+
return;
59+
}
4860
let h = hash(&p.bounded_ty);
4961
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>()) {
5062
let mut hint_string = format!(

clippy_lints/src/utils/conf.rs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ define_Conf! {
156156
(array_size_threshold, "array_size_threshold": u64, 512_000),
157157
/// Lint: VEC_BOX. The size of the boxed type in bytes, where boxing in a `Vec` is allowed
158158
(vec_box_size_threshold, "vec_box_size_threshold": u64, 4096),
159+
/// Lint: TYPE_REPETITION_IN_BOUNDS. The maximum number of bounds a trait can have to be linted
160+
(max_trait_bounds, "max_trait_bounds": u64, 3),
159161
/// Lint: STRUCT_EXCESSIVE_BOOLS. The maximum number of bools a struct can have
160162
(max_struct_bools, "max_struct_bools": u64, 3),
161163
/// Lint: FN_PARAMS_EXCESSIVE_BOOLS. The maximum number of bools function parameters can have
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `third-party` at line 5 column 1
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `third-party` at line 5 column 1
22

33
error: aborting due to previous error
44

tests/ui/type_repetition_in_bounds.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#[deny(clippy::type_repetition_in_bounds)]
1+
#![deny(clippy::type_repetition_in_bounds)]
2+
3+
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
24

35
pub fn foo<T>(_t: T)
46
where
@@ -16,4 +18,21 @@ where
1618
unimplemented!();
1719
}
1820

21+
trait LintBounds
22+
where
23+
Self: Clone,
24+
Self: Copy + Default + Ord,
25+
Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
26+
Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
27+
{
28+
}
29+
30+
trait LotsOfBounds
31+
where
32+
Self: Clone + Copy + Default + Ord,
33+
Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
34+
Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
35+
{
36+
}
37+
1938
fn main() {}
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
error: this type has already been used as a bound predicate
2-
--> $DIR/type_repetition_in_bounds.rs:6:5
2+
--> $DIR/type_repetition_in_bounds.rs:8:5
33
|
44
LL | T: Clone,
55
| ^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/type_repetition_in_bounds.rs:1:8
8+
--> $DIR/type_repetition_in_bounds.rs:1:9
99
|
10-
LL | #[deny(clippy::type_repetition_in_bounds)]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | #![deny(clippy::type_repetition_in_bounds)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= help: consider combining the bounds: `T: Copy + Clone`
1313

14-
error: aborting due to previous error
14+
error: this type has already been used as a bound predicate
15+
--> $DIR/type_repetition_in_bounds.rs:24:5
16+
|
17+
LL | Self: Copy + Default + Ord,
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
21+
22+
error: aborting due to 2 previous errors
1523

0 commit comments

Comments
 (0)