2018-05-28 19:17:55 -05:00
|
|
|
//! lint on inherent implementations
|
|
|
|
|
2021-05-20 05:30:31 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_note;
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::is_lint_allowed;
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-09-12 04:58:27 -05:00
|
|
|
use rustc_hir::{def_id::LocalDefId, Item, ItemKind, Node};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-05-20 05:30:31 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-12-30 18:17:56 -06:00
|
|
|
use rustc_span::Span;
|
2021-05-20 05:30:31 -05:00
|
|
|
use std::collections::hash_map::Entry;
|
2018-05-28 19:17:55 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for multiple inherent implementations of a struct
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Splitting the implementation of a type makes the code harder to navigate.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// }
|
|
|
|
/// impl X {
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct X;
|
|
|
|
/// impl X {
|
|
|
|
/// fn one() {}
|
|
|
|
/// fn other() {}
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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"
|
|
|
|
}
|
|
|
|
|
2021-05-20 05:30:31 -05:00
|
|
|
declare_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 {
|
2021-09-12 04:58:27 -05:00
|
|
|
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
|
2021-05-20 05:30:31 -05:00
|
|
|
// Map from a type to it's first impl block. Needed to distinguish generic arguments.
|
|
|
|
// e.g. `Foo<Bar>` and `Foo<Baz>`
|
|
|
|
let mut type_map = FxHashMap::default();
|
|
|
|
// List of spans to lint. (lint_span, first_span)
|
|
|
|
let mut lint_spans = Vec::new();
|
|
|
|
|
|
|
|
for (_, impl_ids) in cx
|
|
|
|
.tcx
|
|
|
|
.crate_inherent_impls(())
|
|
|
|
.inherent_impls
|
|
|
|
.iter()
|
|
|
|
.filter(|(&id, impls)| {
|
|
|
|
impls.len() > 1
|
|
|
|
// Check for `#[allow]` on the type definition
|
2021-07-15 03:44:10 -05:00
|
|
|
&& !is_lint_allowed(
|
2021-05-20 05:30:31 -05:00
|
|
|
cx,
|
|
|
|
MULTIPLE_INHERENT_IMPL,
|
|
|
|
cx.tcx.hir().local_def_id_to_hir_id(id),
|
|
|
|
)
|
|
|
|
})
|
2020-01-17 23:14:36 -06:00
|
|
|
{
|
2021-05-20 05:30:31 -05:00
|
|
|
for impl_id in impl_ids.iter().map(|id| id.expect_local()) {
|
|
|
|
match type_map.entry(cx.tcx.type_of(impl_id)) {
|
|
|
|
Entry::Vacant(e) => {
|
|
|
|
// Store the id for the first impl block of this type. The span is retrieved lazily.
|
|
|
|
e.insert(IdOrSpan::Id(impl_id));
|
|
|
|
},
|
|
|
|
Entry::Occupied(mut e) => {
|
|
|
|
if let Some(span) = get_impl_span(cx, impl_id) {
|
|
|
|
let first_span = match *e.get() {
|
|
|
|
IdOrSpan::Span(s) => s,
|
|
|
|
IdOrSpan::Id(id) => {
|
|
|
|
if let Some(s) = get_impl_span(cx, id) {
|
|
|
|
// Remember the span of the first block.
|
|
|
|
*e.get_mut() = IdOrSpan::Span(s);
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
// The first impl block isn't considered by the lint. Replace it with the
|
|
|
|
// current one.
|
|
|
|
*e.get_mut() = IdOrSpan::Span(span);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
lint_spans.push((span, first_span));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2019-06-21 01:14:07 -05:00
|
|
|
}
|
2021-05-20 05:30:31 -05:00
|
|
|
|
|
|
|
// Switching to the next type definition, no need to keep the current entries around.
|
|
|
|
type_map.clear();
|
2018-05-28 19:17:55 -05:00
|
|
|
}
|
|
|
|
|
2021-05-20 05:30:31 -05:00
|
|
|
// `TyCtxt::crate_inherent_impls` doesn't have a defined order. Sort the lint output first.
|
|
|
|
lint_spans.sort_by_key(|x| x.0.lo());
|
|
|
|
for (span, first_span) in lint_spans {
|
|
|
|
span_lint_and_note(
|
|
|
|
cx,
|
|
|
|
MULTIPLE_INHERENT_IMPL,
|
|
|
|
span,
|
|
|
|
"multiple implementations of this structure",
|
|
|
|
Some(first_span),
|
|
|
|
"first implementation here",
|
|
|
|
);
|
2018-05-28 19:17:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 05:30:31 -05:00
|
|
|
|
|
|
|
/// Gets the span for the given impl block unless it's not being considered by the lint.
|
|
|
|
fn get_impl_span(cx: &LateContext<'_>, id: LocalDefId) -> Option<Span> {
|
|
|
|
let id = cx.tcx.hir().local_def_id_to_hir_id(id);
|
|
|
|
if let Node::Item(&Item {
|
2022-05-05 07:01:37 -05:00
|
|
|
kind: ItemKind::Impl(impl_item),
|
2021-05-20 05:30:31 -05:00
|
|
|
span,
|
|
|
|
..
|
|
|
|
}) = cx.tcx.hir().get(id)
|
|
|
|
{
|
2021-12-06 05:33:31 -06:00
|
|
|
(!span.from_expansion()
|
|
|
|
&& impl_item.generics.params.is_empty()
|
|
|
|
&& !is_lint_allowed(cx, MULTIPLE_INHERENT_IMPL, id))
|
|
|
|
.then(|| span)
|
2021-05-20 05:30:31 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum IdOrSpan {
|
|
|
|
Id(LocalDefId),
|
|
|
|
Span(Span),
|
|
|
|
}
|