Skip to content

Commit 88cfd70

Browse files
committed
Auto merge of rust-lang#8252 - dswij:8229, r=xFrednet
cover trait for `trait_duplication_in_bounds` closes rust-lang#8229 changelog: [`trait_duplication_in_bounds`] covers trait functions with `Self` bounds
2 parents 5991695 + f4dc348 commit 88cfd70

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::{SpanlessEq, SpanlessHash};
44
use core::hash::{Hash, Hasher};
55
use if_chain::if_chain;
6-
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_data_structures::unhash::UnhashMap;
88
use rustc_errors::Applicability;
9-
use rustc_hir::{def::Res, GenericBound, Generics, ParamName, Path, QPath, Ty, TyKind, WherePredicate};
9+
use rustc_hir::def::Res;
10+
use rustc_hir::{
11+
GenericBound, Generics, Item, ItemKind, Node, ParamName, Path, PathSegment, QPath, TraitItem, Ty, TyKind,
12+
WherePredicate,
13+
};
1014
use rustc_lint::{LateContext, LateLintPass};
1115
use rustc_session::{declare_tool_lint, impl_lint_pass};
1216
use rustc_span::Span;
@@ -84,6 +88,53 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
8488
self.check_type_repetition(cx, gen);
8589
check_trait_bound_duplication(cx, gen);
8690
}
91+
92+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
93+
let Generics { where_clause, .. } = &item.generics;
94+
let mut self_bounds_set = FxHashSet::default();
95+
96+
for predicate in where_clause.predicates {
97+
if_chain! {
98+
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
99+
if !bound_predicate.span.from_expansion();
100+
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
101+
if let Some(PathSegment { res: Some(Res::SelfTy(Some(def_id), _)), .. }) = segments.first();
102+
103+
if let Some(
104+
Node::Item(
105+
Item {
106+
kind: ItemKind::Trait(_, _, _, self_bounds, _),
107+
.. }
108+
)
109+
) = cx.tcx.hir().get_if_local(*def_id);
110+
then {
111+
if self_bounds_set.is_empty() {
112+
for bound in self_bounds.iter() {
113+
let Some((self_res, _)) = get_trait_res_span_from_bound(bound) else { continue };
114+
self_bounds_set.insert(self_res);
115+
}
116+
}
117+
118+
bound_predicate
119+
.bounds
120+
.iter()
121+
.filter_map(get_trait_res_span_from_bound)
122+
.for_each(|(trait_item_res, span)| {
123+
if self_bounds_set.get(&trait_item_res).is_some() {
124+
span_lint_and_help(
125+
cx,
126+
TRAIT_DUPLICATION_IN_BOUNDS,
127+
span,
128+
"this trait bound is already specified in trait declaration",
129+
None,
130+
"consider removing this trait bound",
131+
);
132+
}
133+
});
134+
}
135+
}
136+
}
137+
}
87138
}
88139

89140
fn get_trait_res_span_from_bound(bound: &GenericBound<'_>) -> Option<(Res, Span)> {

tests/ui/trait_duplication_in_bounds.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,49 @@ where
2828
unimplemented!();
2929
}
3030

31+
trait T: Default {
32+
fn f()
33+
where
34+
Self: Default;
35+
}
36+
37+
trait U: Default {
38+
fn f()
39+
where
40+
Self: Clone;
41+
}
42+
43+
trait ZZ: Default {
44+
fn g();
45+
fn h();
46+
fn f()
47+
where
48+
Self: Default + Clone;
49+
}
50+
51+
trait BadTrait: Default + Clone {
52+
fn f()
53+
where
54+
Self: Default + Clone;
55+
fn g()
56+
where
57+
Self: Default;
58+
fn h()
59+
where
60+
Self: Copy;
61+
}
62+
63+
#[derive(Default, Clone)]
64+
struct Life {}
65+
66+
impl T for Life {
67+
// this should not warn
68+
fn f() {}
69+
}
70+
71+
impl U for Life {
72+
// this should not warn
73+
fn f() {}
74+
}
75+
3176
fn main() {}

tests/ui/trait_duplication_in_bounds.stderr

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,45 @@ LL | fn bad_foo<T: Clone + Default, Z: Copy>(arg0: T, arg1: Z)
1919
|
2020
= help: consider removing this trait bound
2121

22-
error: aborting due to 2 previous errors
22+
error: this trait bound is already specified in trait declaration
23+
--> $DIR/trait_duplication_in_bounds.rs:34:15
24+
|
25+
LL | Self: Default;
26+
| ^^^^^^^
27+
|
28+
= help: consider removing this trait bound
29+
30+
error: this trait bound is already specified in trait declaration
31+
--> $DIR/trait_duplication_in_bounds.rs:48:15
32+
|
33+
LL | Self: Default + Clone;
34+
| ^^^^^^^
35+
|
36+
= help: consider removing this trait bound
37+
38+
error: this trait bound is already specified in trait declaration
39+
--> $DIR/trait_duplication_in_bounds.rs:54:15
40+
|
41+
LL | Self: Default + Clone;
42+
| ^^^^^^^
43+
|
44+
= help: consider removing this trait bound
45+
46+
error: this trait bound is already specified in trait declaration
47+
--> $DIR/trait_duplication_in_bounds.rs:54:25
48+
|
49+
LL | Self: Default + Clone;
50+
| ^^^^^
51+
|
52+
= help: consider removing this trait bound
53+
54+
error: this trait bound is already specified in trait declaration
55+
--> $DIR/trait_duplication_in_bounds.rs:57:15
56+
|
57+
LL | Self: Default;
58+
| ^^^^^^^
59+
|
60+
= help: consider removing this trait bound
61+
62+
error: aborting due to 7 previous errors
2363

0 commit comments

Comments
 (0)