1 //! lint on inherent implementations
5 use std::collections::HashMap;
6 use std::default::Default;
9 /// **What it does:** Checks for multiple inherent implementations of a struct
11 /// **Why is this bad?** Splitting the implementation of a type makes the code harder to navigate.
13 /// **Known problems:** None.
35 declare_clippy_lint! {
36 pub MULTIPLE_INHERENT_IMPL,
38 "Multiple inherent impl that could be grouped"
42 impls: HashMap<def_id::DefId, (Span, Generics)>,
45 impl Default for Pass {
46 fn default() -> Self {
47 Pass { impls: HashMap::new() }
51 impl LintPass for Pass {
52 fn get_lints(&self) -> LintArray {
53 lint_array!(MULTIPLE_INHERENT_IMPL)
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
58 fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
59 if let ItemKind::Impl(_, _, _, ref generics, None, _, _) = item.node {
60 // Remember for each inherent implementation encoutered its span and generics
62 .insert(item.hir_id.owner_def_id(), (item.span, generics.clone()));
66 fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx Crate) {
67 if let Some(item) = krate.items.values().nth(0) {
68 // Retrieve all inherent implementations from the crate, grouped by type
71 .crate_inherent_impls(item.hir_id.owner_def_id().krate)
75 // Filter out implementations that have generic params (type or lifetime)
76 let mut impl_spans = impls
78 .filter_map(|impl_def| self.impls.get(impl_def))
79 .filter(|(_, generics)| generics.params.len() == 0)
80 .map(|(span, _)| span);
81 if let Some(initial_span) = impl_spans.nth(0) {
82 impl_spans.for_each(|additional_span| {
84 MULTIPLE_INHERENT_IMPL,
86 "Multiple implementations of this structure",
88 "First implementation here",