diff options
| author | dswij <dswijj@gmail.com> | 2022-01-10 12:45:01 +0800 |
|---|---|---|
| committer | dswij <dswijj@gmail.com> | 2022-01-10 12:45:22 +0800 |
| commit | f4dc348ad580cb5958f724a1a5ac7b538ef35515 (patch) | |
| tree | fac2e3ffa1c8ef5209b2d16976bc7857f68f6fc1 | |
| parent | f690978023d7465d05abe9a1288100f192e74193 (diff) | |
| download | rust-f4dc348ad580cb5958f724a1a5ac7b538ef35515.tar.gz rust-f4dc348ad580cb5958f724a1a5ac7b538ef35515.zip | |
`trait_duplication_in_bounds` Update description and add test
| -rw-r--r-- | clippy_lints/src/trait_bounds.rs | 29 | ||||
| -rw-r--r-- | tests/ui/trait_duplication_in_bounds.rs | 8 | ||||
| -rw-r--r-- | tests/ui/trait_duplication_in_bounds.stderr | 24 |
3 files changed, 40 insertions, 21 deletions
diff --git a/clippy_lints/src/trait_bounds.rs b/clippy_lints/src/trait_bounds.rs index c91b24462c0..6369aafe3f9 100644 --- a/clippy_lints/src/trait_bounds.rs +++ b/clippy_lints/src/trait_bounds.rs @@ -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", - ); -} diff --git a/tests/ui/trait_duplication_in_bounds.rs b/tests/ui/trait_duplication_in_bounds.rs index 3e490126273..2edb202892a 100644 --- a/tests/ui/trait_duplication_in_bounds.rs +++ b/tests/ui/trait_duplication_in_bounds.rs @@ -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)] diff --git a/tests/ui/trait_duplication_in_bounds.stderr b/tests/ui/trait_duplication_in_bounds.stderr index 6326139c187..e0c7a7ec618 100644 --- a/tests/ui/trait_duplication_in_bounds.stderr +++ b/tests/ui/trait_duplication_in_bounds.stderr @@ -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 |
