Skip to content

Commit 72817c4

Browse files
committed
filter
1 parent a87be2f commit 72817c4

File tree

1 file changed

+64
-63
lines changed

1 file changed

+64
-63
lines changed

clippy_lints/src/same_name_method.rs

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -65,77 +65,78 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
6565
}) = &item.kind
6666
{
6767
if let TyKind::Path(QPath::Resolved(_, Path { res, .. })) = self_ty.kind {
68-
for impl_item_ref in *items {
69-
if matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. }) {
70-
let method_name = impl_item_ref.ident.as_str();
71-
match of_trait {
72-
Some(_trait_ref) => {
73-
// FIXME: try to collect default methods in trait.
74-
// but `rustc_middle::ty::assoc::AssocItems::items` is not public
75-
// if let Some(node)=cx.tcx.hir().find(trait_ref.hir_ref_id){
76-
// if let Node::TraitRef(TraitRef{path,..})=node{
77-
// if let Res::Def(DefKind::Trait,did)=path.res{
78-
// items is private
79-
// cx.tcx.associated_items(did).items.iter().filter(|_k,v|matches!(v,AssocKind::
80-
// Fn)). map(|(k,_v)|k).collect(); }
81-
// }
82-
// }
68+
for impl_item_ref in (*items)
69+
.iter()
70+
.filter(|impl_item_ref| matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. }))
71+
{
72+
let method_name = impl_item_ref.ident.as_str();
73+
match of_trait {
74+
Some(_trait_ref) => {
75+
// FIXME: try to collect default methods in trait.
76+
// but `rustc_middle::ty::assoc::AssocItems::items` is not public
77+
// if let Some(node)=cx.tcx.hir().find(trait_ref.hir_ref_id){
78+
// if let Node::TraitRef(TraitRef{path,..})=node{
79+
// if let Res::Def(DefKind::Trait,did)=path.res{
80+
// items is private
81+
// cx.tcx.associated_items(did).items.iter().filter(|_k,v|matches!(v,AssocKind::
82+
// Fn)). map(|(k,_v)|k).collect(); }
83+
// }
84+
// }
8385

84-
let trait_span = impl_item_ref.span;
85-
if let Some(s) = mp.get_mut(res) {
86-
if let Some(impl_span) = s.impl_methods.get(&method_name) {
87-
span_lint_and_then(
88-
cx,
89-
SAME_NAME_METHOD,
90-
*impl_span,
91-
"binding's name is same to an existing binding",
92-
|diag| {
93-
diag.span_note(trait_span, "existing binding defined here");
94-
},
95-
);
96-
}
97-
if let Some(v) = s.trait_methods.get_mut(&method_name) {
98-
v.push(trait_span);
99-
} else {
100-
s.trait_methods.insert(method_name, vec![trait_span]);
101-
}
102-
} else {
103-
mp.insert(
104-
*res,
105-
S {
106-
impl_methods: BTreeMap::new(),
107-
trait_methods: BTreeMap::from([(method_name, vec![trait_span])]),
86+
let trait_span = impl_item_ref.span;
87+
if let Some(s) = mp.get_mut(res) {
88+
if let Some(impl_span) = s.impl_methods.get(&method_name) {
89+
span_lint_and_then(
90+
cx,
91+
SAME_NAME_METHOD,
92+
*impl_span,
93+
"binding's name is same to an existing binding",
94+
|diag| {
95+
diag.span_note(trait_span, "existing binding defined here");
10896
},
10997
);
11098
}
111-
},
112-
None => {
113-
let impl_span = impl_item_ref.span;
114-
if let Some(s) = mp.get_mut(res) {
115-
if let Some(trait_spans) = s.trait_methods.get(&method_name) {
116-
span_lint_and_then(
117-
cx,
118-
SAME_NAME_METHOD,
119-
impl_span,
120-
"binding's name is same to an existing binding",
121-
|diag| {
122-
// TODO should we `span_note` on every trait?
123-
diag.span_note(trait_spans[0], "existing binding defined here");
124-
},
125-
);
126-
}
127-
s.impl_methods.insert(method_name, impl_span);
99+
if let Some(v) = s.trait_methods.get_mut(&method_name) {
100+
v.push(trait_span);
128101
} else {
129-
mp.insert(
130-
*res,
131-
S {
132-
impl_methods: BTreeMap::from([(method_name, impl_span)]),
133-
trait_methods: BTreeMap::new(),
102+
s.trait_methods.insert(method_name, vec![trait_span]);
103+
}
104+
} else {
105+
mp.insert(
106+
*res,
107+
S {
108+
impl_methods: BTreeMap::new(),
109+
trait_methods: BTreeMap::from([(method_name, vec![trait_span])]),
110+
},
111+
);
112+
}
113+
},
114+
None => {
115+
let impl_span = impl_item_ref.span;
116+
if let Some(s) = mp.get_mut(res) {
117+
if let Some(trait_spans) = s.trait_methods.get(&method_name) {
118+
span_lint_and_then(
119+
cx,
120+
SAME_NAME_METHOD,
121+
impl_span,
122+
"binding's name is same to an existing binding",
123+
|diag| {
124+
// TODO should we `span_note` on every trait?
125+
diag.span_note(trait_spans[0], "existing binding defined here");
134126
},
135127
);
136128
}
137-
},
138-
}
129+
s.impl_methods.insert(method_name, impl_span);
130+
} else {
131+
mp.insert(
132+
*res,
133+
S {
134+
impl_methods: BTreeMap::from([(method_name, impl_span)]),
135+
trait_methods: BTreeMap::new(),
136+
},
137+
);
138+
}
139+
},
139140
}
140141
}
141142
}

0 commit comments

Comments
 (0)