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