]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inherent_impl.rs
Rustup
[rust.git] / clippy_lints / src / inherent_impl.rs
1 //! lint on inherent implementations
2
3 use rustc::hir::*;
4 use rustc::lint::*;
5 use std::collections::HashMap;
6 use std::default::Default;
7 use syntax_pos::Span;
8
9 /// **What it does:** Checks for multiple inherent implementations of a struct
10 ///
11 /// **Why is this bad?** Splitting the implementation of a type makes the code harder to navigate.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// struct X;
18 /// impl X {
19 ///     fn one() {}
20 /// }
21 /// impl X {
22 ///     fn other() {}
23 /// }
24 /// ```
25 ///
26 /// Could be written:
27 ///
28 /// ```rust
29 /// struct X;
30 /// impl X {
31 ///     fn one() {}
32 ///     fn other() {}
33 /// }
34 /// ```
35 declare_clippy_lint! {
36     pub MULTIPLE_INHERENT_IMPL,
37     restriction,
38     "Multiple inherent impl that could be grouped"
39 }
40
41 pub struct Pass {
42     impls: HashMap<def_id::DefId, (Span, Generics)>,
43 }
44
45 impl Default for Pass {
46     fn default() -> Self {
47         Pass { impls: HashMap::new() }
48     }
49 }
50
51 impl LintPass for Pass {
52     fn get_lints(&self) -> LintArray {
53         lint_array!(MULTIPLE_INHERENT_IMPL)
54     }
55 }
56
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
58     fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
59         if let Item_::ItemImpl(_, _, _, ref generics, None, _, _) = item.node {
60             // Remember for each inherent implementation encoutered its span and generics
61             self.impls
62                 .insert(item.hir_id.owner_def_id(), (item.span, generics.clone()));
63         }
64     }
65
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
69             for impls in cx
70                 .tcx
71                 .crate_inherent_impls(item.hir_id.owner_def_id().krate)
72                 .inherent_impls
73                 .values()
74             {
75                 // Filter out implementations that have generic params (type or lifetime)
76                 let mut impl_spans = impls
77                     .iter()
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| {
83                         cx.span_lint_note(
84                             MULTIPLE_INHERENT_IMPL,
85                             *additional_span,
86                             "Multiple implementations of this structure",
87                             *initial_span,
88                             "First implementation here",
89                         )
90                     })
91                 }
92             }
93         }
94     }
95 }