-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathsame_name_method.rs
More file actions
152 lines (143 loc) · 6.48 KB
/
same_name_method.rs
File metadata and controls
152 lines (143 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use clippy_utils::diagnostics::span_lint_hir_and_then;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{AssocItem, AssocKind};
use rustc_session::declare_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::Symbol;
use std::collections::{BTreeMap, BTreeSet};
declare_clippy_lint! {
/// ### What it does
/// It lints if a struct has two methods with the same name:
/// one from a trait, another not from a trait.
///
/// ### Why restrict this?
/// Confusing.
///
/// ### Example
/// ```no_run
/// trait T {
/// fn foo(&self) {}
/// }
///
/// struct S;
///
/// impl T for S {
/// fn foo(&self) {}
/// }
///
/// impl S {
/// fn foo(&self) {}
/// }
/// ```
#[clippy::version = "1.57.0"]
pub SAME_NAME_METHOD,
restriction,
"two method with same name"
}
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
struct ExistingName {
impl_methods: BTreeMap<Symbol, (Span, HirId)>,
trait_methods: BTreeMap<Symbol, Vec<Span>>,
}
impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
let mut map = FxHashMap::<Res, ExistingName>::default();
for id in cx.tcx.hir_free_items() {
if matches!(cx.tcx.def_kind(id.owner_id), DefKind::Impl { .. })
&& let item = cx.tcx.hir_item(id)
&& let ItemKind::Impl(Impl { of_trait, self_ty, .. }) = &item.kind
&& let TyKind::Path(QPath::Resolved(_, Path { res, .. })) = self_ty.kind
{
if !map.contains_key(res) {
map.insert(
*res,
ExistingName {
impl_methods: BTreeMap::new(),
trait_methods: BTreeMap::new(),
},
);
}
let existing_name = map.get_mut(res).unwrap();
match of_trait {
Some(of_trait) => {
let mut methods_in_trait: BTreeSet<Symbol> = if let Node::TraitRef(TraitRef { path, .. }) =
cx.tcx.hir_node(of_trait.trait_ref.hir_ref_id)
&& let Res::Def(DefKind::Trait, did) = path.res
{
// FIXME: if
// `rustc_middle::ty::assoc::AssocItems::items` is public,
// we can iterate its keys instead of `in_definition_order`,
// which's more efficient
cx.tcx
.associated_items(did)
.in_definition_order()
.filter(|assoc_item| assoc_item.is_fn())
.map(AssocItem::name)
.collect()
} else {
BTreeSet::new()
};
let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
span_lint_hir_and_then(
cx,
SAME_NAME_METHOD,
*hir_id,
*impl_span,
"method's name is the same as an existing method in a trait",
|diag| {
diag.span_note(
trait_method_span,
format!("existing `{method_name}` defined here"),
);
},
);
}
if let Some(v) = existing_name.trait_methods.get_mut(&method_name) {
v.push(trait_method_span);
} else {
existing_name.trait_methods.insert(method_name, vec![trait_method_span]);
}
};
for assoc_item in cx.tcx.associated_items(id.owner_id).in_definition_order() {
if let AssocKind::Fn { name, .. } = assoc_item.kind {
methods_in_trait.remove(&name);
check_trait_method(name, cx.tcx.def_span(assoc_item.def_id));
}
}
for method_name in methods_in_trait {
check_trait_method(method_name, item.span);
}
},
None => {
for assoc_item in cx.tcx.associated_items(id.owner_id).in_definition_order() {
let AssocKind::Fn { name, .. } = assoc_item.kind else {
continue;
};
let impl_span = cx.tcx.def_span(assoc_item.def_id);
let hir_id = cx.tcx.local_def_id_to_hir_id(assoc_item.def_id.expect_local());
if let Some(trait_spans) = existing_name.trait_methods.get(&name) {
span_lint_hir_and_then(
cx,
SAME_NAME_METHOD,
hir_id,
impl_span,
"method's name is the same as an existing method in a trait",
|diag| {
// TODO should we `span_note` on every trait?
// iterate on trait_spans?
diag.span_note(trait_spans[0], format!("existing `{name}` defined here"));
},
);
}
existing_name.impl_methods.insert(name, (impl_span, hir_id));
}
},
}
}
}
}
}