2018-05-28 19:17:55 -05:00
|
|
|
//! lint on inherent implementations
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::in_macro;
|
2021-04-08 10:50:13 -05:00
|
|
|
use rustc_hir::def_id::DefIdMap;
|
2021-05-11 06:39:19 -05:00
|
|
|
use rustc_hir::{Crate, Impl, Item, ItemKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2019-12-30 18:17:56 -06:00
|
|
|
use rustc_span::Span;
|
2018-05-28 19:17:55 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for multiple inherent implementations of a struct
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Splitting the implementation of a type makes the code harder to navigate.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// }
|
|
|
|
/// impl X {
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-05-28 19:17:55 -05:00
|
|
|
pub MULTIPLE_INHERENT_IMPL,
|
2018-05-29 03:19:16 -05:00
|
|
|
restriction,
|
2018-05-28 19:17:55 -05:00
|
|
|
"Multiple inherent impl that could be grouped"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
#[allow(clippy::module_name_repetitions)]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MultipleInherentImpl {
|
2021-04-08 10:50:13 -05:00
|
|
|
impls: DefIdMap<Span>,
|
2018-05-28 19:17:55 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
|
2018-05-28 19:17:55 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
|
|
|
|
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2020-11-22 16:46:21 -06:00
|
|
|
if let ItemKind::Impl(Impl {
|
2020-01-17 23:14:36 -06:00
|
|
|
ref generics,
|
|
|
|
of_trait: None,
|
|
|
|
..
|
2020-11-22 16:46:21 -06:00
|
|
|
}) = item.kind
|
2020-01-17 23:14:36 -06:00
|
|
|
{
|
2020-07-26 14:07:07 -05:00
|
|
|
// Remember for each inherent implementation encountered its span and generics
|
2019-06-21 01:14:07 -05:00
|
|
|
// but filter out implementations that have generic params (type or lifetime)
|
2019-09-27 20:46:55 -05:00
|
|
|
// or are derived from a macro
|
2019-12-31 01:50:43 -06:00
|
|
|
if !in_macro(item.span) && generics.params.is_empty() {
|
2021-01-30 10:47:51 -06:00
|
|
|
self.impls.insert(item.def_id.to_def_id(), item.span);
|
2019-06-21 01:14:07 -05:00
|
|
|
}
|
2018-05-28 19:17:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'_>) {
|
2021-01-30 10:47:51 -06:00
|
|
|
if !krate.items.is_empty() {
|
2018-05-28 19:17:55 -05:00
|
|
|
// Retrieve all inherent implementations from the crate, grouped by type
|
2021-05-11 06:39:19 -05:00
|
|
|
for impls in cx.tcx.crate_inherent_impls(()).inherent_impls.values() {
|
2018-05-28 19:17:55 -05:00
|
|
|
// Filter out implementations that have generic params (type or lifetime)
|
2019-06-21 01:14:07 -05:00
|
|
|
let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def));
|
2019-12-28 16:31:41 -06:00
|
|
|
if let Some(initial_span) = impl_spans.next() {
|
2018-05-28 19:17:55 -05:00
|
|
|
impl_spans.for_each(|additional_span| {
|
2018-08-28 06:13:42 -05:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2018-05-28 19:17:55 -05:00
|
|
|
MULTIPLE_INHERENT_IMPL,
|
|
|
|
*additional_span,
|
2020-08-28 09:10:16 -05:00
|
|
|
"multiple implementations of this structure",
|
2020-04-17 01:08:00 -05:00
|
|
|
|diag| {
|
2020-08-28 09:10:16 -05:00
|
|
|
diag.span_note(*initial_span, "first implementation here");
|
2018-08-28 06:13:42 -05:00
|
|
|
},
|
2018-05-28 19:17:55 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|