2022-06-16 15:39:06 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
2021-09-28 17:03:12 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2022-06-16 15:39:06 +00:00
|
|
|
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
|
2021-09-28 17:03:12 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty::AssocKind;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
use rustc_span::symbol::Symbol;
|
|
|
|
use rustc_span::Span;
|
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2021-12-06 11:33:31 +00:00
|
|
|
/// It lints if a struct has two methods with the same name:
|
2021-09-28 17:03:12 +00:00
|
|
|
/// one from a trait, another not from trait.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Confusing.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
|
|
|
/// trait T {
|
|
|
|
/// fn foo(&self) {}
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct S;
|
|
|
|
///
|
|
|
|
/// impl T for S {
|
|
|
|
/// fn foo(&self) {}
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl S {
|
|
|
|
/// fn foo(&self) {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-09-28 17:03:12 +00:00
|
|
|
pub SAME_NAME_METHOD,
|
|
|
|
restriction,
|
|
|
|
"two method with same name"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
|
|
|
|
|
|
|
|
struct ExistingName {
|
2022-06-16 15:39:06 +00:00
|
|
|
impl_methods: BTreeMap<Symbol, (Span, HirId)>,
|
2021-09-28 17:03:12 +00:00
|
|
|
trait_methods: BTreeMap<Symbol, Vec<Span>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
|
2022-06-16 15:39:06 +00:00
|
|
|
#[expect(clippy::too_many_lines)]
|
2021-09-12 09:58:27 +00:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
2021-09-28 17:03:12 +00:00
|
|
|
let mut map = FxHashMap::<Res, ExistingName>::default();
|
|
|
|
|
2022-04-03 19:50:33 +00:00
|
|
|
for id in cx.tcx.hir().items() {
|
2022-04-29 17:11:22 +00:00
|
|
|
if matches!(cx.tcx.def_kind(id.def_id), DefKind::Impl)
|
2022-05-05 14:12:52 +00:00
|
|
|
&& let item = cx.tcx.hir().item(id)
|
|
|
|
&& let ItemKind::Impl(Impl {
|
2022-04-29 17:11:22 +00:00
|
|
|
items,
|
|
|
|
of_trait,
|
|
|
|
self_ty,
|
|
|
|
..
|
|
|
|
}) = &item.kind
|
2022-05-05 14:12:52 +00:00
|
|
|
&& let TyKind::Path(QPath::Resolved(_, Path { res, .. })) = self_ty.kind
|
2021-09-28 17:03:12 +00:00
|
|
|
{
|
2022-05-05 14:12:52 +00:00
|
|
|
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();
|
2021-09-28 17:03:12 +00:00
|
|
|
|
2022-05-05 14:12:52 +00:00
|
|
|
match of_trait {
|
|
|
|
Some(trait_ref) => {
|
|
|
|
let mut methods_in_trait: BTreeSet<Symbol> = if_chain! {
|
|
|
|
if let Some(Node::TraitRef(TraitRef { path, .. })) =
|
|
|
|
cx.tcx.hir().find(trait_ref.hir_ref_id);
|
|
|
|
if let Res::Def(DefKind::Trait, did) = path.res;
|
|
|
|
then{
|
|
|
|
// 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| {
|
|
|
|
matches!(assoc_item.kind, AssocKind::Fn)
|
|
|
|
})
|
|
|
|
.map(|assoc_item| assoc_item.name)
|
|
|
|
.collect()
|
|
|
|
}else{
|
|
|
|
BTreeSet::new()
|
2021-09-28 17:03:12 +00:00
|
|
|
}
|
2022-05-05 14:12:52 +00:00
|
|
|
};
|
2021-09-28 17:03:12 +00:00
|
|
|
|
2022-05-05 14:12:52 +00:00
|
|
|
let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
|
2022-06-16 15:39:06 +00:00
|
|
|
if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
|
|
|
|
span_lint_hir_and_then(
|
2022-05-05 14:12:52 +00:00
|
|
|
cx,
|
|
|
|
SAME_NAME_METHOD,
|
2022-06-16 15:39:06 +00:00
|
|
|
*hir_id,
|
2022-05-05 14:12:52 +00:00
|
|
|
*impl_span,
|
|
|
|
"method's name is the same as an existing method in a trait",
|
|
|
|
|diag| {
|
|
|
|
diag.span_note(
|
|
|
|
trait_method_span,
|
|
|
|
&format!("existing `{}` defined here", method_name),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2021-09-28 17:03:12 +00:00
|
|
|
}
|
2022-05-05 14:12:52 +00:00
|
|
|
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]);
|
2021-09-28 17:03:12 +00:00
|
|
|
}
|
2022-05-05 14:12:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for impl_item_ref in (*items).iter().filter(|impl_item_ref| {
|
|
|
|
matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. })
|
|
|
|
}) {
|
|
|
|
let method_name = impl_item_ref.ident.name;
|
|
|
|
methods_in_trait.remove(&method_name);
|
|
|
|
check_trait_method(method_name, impl_item_ref.span);
|
|
|
|
}
|
|
|
|
|
|
|
|
for method_name in methods_in_trait {
|
|
|
|
check_trait_method(method_name, item.span);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
for impl_item_ref in (*items).iter().filter(|impl_item_ref| {
|
|
|
|
matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. })
|
|
|
|
}) {
|
|
|
|
let method_name = impl_item_ref.ident.name;
|
|
|
|
let impl_span = impl_item_ref.span;
|
2022-06-16 15:39:06 +00:00
|
|
|
let hir_id = impl_item_ref.id.hir_id();
|
2022-05-05 14:12:52 +00:00
|
|
|
if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
|
2022-06-16 15:39:06 +00:00
|
|
|
span_lint_hir_and_then(
|
2022-05-05 14:12:52 +00:00
|
|
|
cx,
|
|
|
|
SAME_NAME_METHOD,
|
2022-06-16 15:39:06 +00:00
|
|
|
hir_id,
|
2022-05-05 14:12:52 +00:00
|
|
|
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 `{}` defined here", method_name),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-06-16 15:39:06 +00:00
|
|
|
existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
|
2022-05-05 14:12:52 +00:00
|
|
|
}
|
|
|
|
},
|
2021-09-28 17:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|