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