trait_duplication_in_bounds Update description and add test

This commit is contained in:
dswij 2022-01-10 12:45:01 +08:00
parent f690978023
commit f4dc348ad5
3 changed files with 40 additions and 21 deletions

View File

@ -121,7 +121,14 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
.filter_map(get_trait_res_span_from_bound)
.for_each(|(trait_item_res, span)| {
if self_bounds_set.get(&trait_item_res).is_some() {
emit_lint(cx, span);
span_lint_and_help(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
span,
"this trait bound is already specified in trait declaration",
None,
"consider removing this trait bound",
);
}
});
}
@ -242,21 +249,17 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
if let Some((_, span_direct)) = trait_resolutions_direct
.iter()
.find(|(res_direct, _)| *res_direct == res_where) {
emit_lint(cx, *span_direct);
span_lint_and_help(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
*span_direct,
"this trait bound is already specified in the where clause",
None,
"consider removing this trait bound",
);
}
}
}
}
}
}
fn emit_lint(cx: &LateContext<'_>, span: Span) {
span_lint_and_help(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
span,
"this trait bound is already specified in the where clause",
None,
"consider removing this trait bound",
);
}

View File

@ -41,6 +41,8 @@ trait U: Default {
}
trait ZZ: Default {
fn g();
fn h();
fn f()
where
Self: Default + Clone;
@ -50,6 +52,12 @@ trait BadTrait: Default + Clone {
fn f()
where
Self: Default + Clone;
fn g()
where
Self: Default;
fn h()
where
Self: Copy;
}
#[derive(Default, Clone)]

View File

@ -19,7 +19,7 @@ LL | fn bad_foo<T: Clone + Default, Z: Copy>(arg0: T, arg1: Z)
|
= help: consider removing this trait bound
error: this trait bound is already specified in the where clause
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:34:15
|
LL | Self: Default;
@ -27,29 +27,37 @@ LL | Self: Default;
|
= help: consider removing this trait bound
error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:46:15
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:48:15
|
LL | Self: Default + Clone;
| ^^^^^^^
|
= help: consider removing this trait bound
error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:52:15
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:54:15
|
LL | Self: Default + Clone;
| ^^^^^^^
|
= help: consider removing this trait bound
error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:52:25
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:54:25
|
LL | Self: Default + Clone;
| ^^^^^
|
= help: consider removing this trait bound
error: aborting due to 6 previous errors
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:57:15
|
LL | Self: Default;
| ^^^^^^^
|
= help: consider removing this trait bound
error: aborting due to 7 previous errors