2018-05-29 00:17:55 +00:00
|
|
|
//! lint on inherent implementations
|
|
|
|
|
2019-09-28 01:46:55 +00:00
|
|
|
use crate::utils::{in_macro, span_lint_and_then};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{def_id, Crate, Item, ItemKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2019-12-31 00:17:56 +00:00
|
|
|
use rustc_span::Span;
|
2018-05-29 00:17:55 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00: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-29 00:17:55 +00:00
|
|
|
pub MULTIPLE_INHERENT_IMPL,
|
2018-05-29 08:19:16 +00:00
|
|
|
restriction,
|
2018-05-29 00:17:55 +00:00
|
|
|
"Multiple inherent impl that could be grouped"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
#[allow(clippy::module_name_repetitions)]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MultipleInherentImpl {
|
2019-06-21 06:14:07 +00:00
|
|
|
impls: FxHashMap<def_id::DefId, Span>,
|
2018-05-29 00:17:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
|
2018-05-29 00:17:55 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
|
|
|
|
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2020-01-18 05:14:36 +00:00
|
|
|
if let ItemKind::Impl {
|
|
|
|
ref generics,
|
|
|
|
of_trait: None,
|
|
|
|
..
|
|
|
|
} = item.kind
|
|
|
|
{
|
2020-07-16 23:58:41 +00:00
|
|
|
// Remember for each inherent implementation encountered its span and generics
|
2019-06-21 06:14:07 +00:00
|
|
|
// but filter out implementations that have generic params (type or lifetime)
|
2019-09-28 01:46:55 +00:00
|
|
|
// or are derived from a macro
|
2019-12-31 07:50:43 +00:00
|
|
|
if !in_macro(item.span) && generics.params.is_empty() {
|
2020-03-19 13:33:10 +00:00
|
|
|
self.impls.insert(item.hir_id.owner.to_def_id(), item.span);
|
2019-06-21 06:14:07 +00:00
|
|
|
}
|
2018-05-29 00:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'_>) {
|
2019-12-28 22:31:41 +00:00
|
|
|
if let Some(item) = krate.items.values().next() {
|
2018-05-29 00:17:55 +00:00
|
|
|
// Retrieve all inherent implementations from the crate, grouped by type
|
|
|
|
for impls in cx
|
|
|
|
.tcx
|
2020-03-19 13:33:10 +00:00
|
|
|
.crate_inherent_impls(item.hir_id.owner.to_def_id().krate)
|
2018-05-29 00:17:55 +00:00
|
|
|
.inherent_impls
|
|
|
|
.values()
|
|
|
|
{
|
|
|
|
// Filter out implementations that have generic params (type or lifetime)
|
2019-06-21 06:14:07 +00:00
|
|
|
let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def));
|
2019-12-28 22:31:41 +00:00
|
|
|
if let Some(initial_span) = impl_spans.next() {
|
2018-05-29 00:17:55 +00:00
|
|
|
impl_spans.for_each(|additional_span| {
|
2018-08-28 11:13:42 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2018-05-29 00:17:55 +00:00
|
|
|
MULTIPLE_INHERENT_IMPL,
|
|
|
|
*additional_span,
|
2020-08-11 13:28:51 +00:00
|
|
|
"multiple implementations of this structure",
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
2020-08-11 13:28:51 +00:00
|
|
|
diag.span_note(*initial_span, "first implementation here");
|
2018-08-28 11:13:42 +00:00
|
|
|
},
|
2018-05-29 00:17:55 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|