]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inherent_impl.rs
Merge pull request #3224 from matthiaskrgr/clippy_self__use_self
[rust.git] / clippy_lints / src / inherent_impl.rs
1 //! lint on inherent implementations
2
3 use crate::rustc::hir::*;
4 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use crate::rustc::{declare_tool_lint, lint_array};
6 use crate::rustc_data_structures::fx::FxHashMap;
7 use std::default::Default;
8 use crate::syntax_pos::Span;
9 use crate::utils::span_lint_and_then;
10
11 /// **What it does:** Checks for multiple inherent implementations of a struct
12 ///
13 /// **Why is this bad?** Splitting the implementation of a type makes the code harder to navigate.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:**
18 /// ```rust
19 /// struct X;
20 /// impl X {
21 ///     fn one() {}
22 /// }
23 /// impl X {
24 ///     fn other() {}
25 /// }
26 /// ```
27 ///
28 /// Could be written:
29 ///
30 /// ```rust
31 /// struct X;
32 /// impl X {
33 ///     fn one() {}
34 ///     fn other() {}
35 /// }
36 /// ```
37 declare_clippy_lint! {
38     pub MULTIPLE_INHERENT_IMPL,
39     restriction,
40     "Multiple inherent impl that could be grouped"
41 }
42
43 pub struct Pass {
44     impls: FxHashMap<def_id::DefId, (Span, Generics)>,
45 }
46
47 impl Default for Pass {
48     fn default() -> Self {
49         Self { impls: FxHashMap::default() }
50     }
51 }
52
53 impl LintPass for Pass {
54     fn get_lints(&self) -> LintArray {
55         lint_array!(MULTIPLE_INHERENT_IMPL)
56     }
57 }
58
59 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
60     fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) {
61         if let ItemKind::Impl(_, _, _, ref generics, None, _, _) = item.node {
62             // Remember for each inherent implementation encoutered its span and generics
63             self.impls
64                 .insert(item.hir_id.owner_def_id(), (item.span, generics.clone()));
65         }
66     }
67
68     fn check_crate_post(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx Crate) {
69         if let Some(item) = krate.items.values().nth(0) {
70             // Retrieve all inherent implementations from the crate, grouped by type
71             for impls in cx
72                 .tcx
73                 .crate_inherent_impls(item.hir_id.owner_def_id().krate)
74                 .inherent_impls
75                 .values()
76             {
77                 // Filter out implementations that have generic params (type or lifetime)
78                 let mut impl_spans = impls
79                     .iter()
80                     .filter_map(|impl_def| self.impls.get(impl_def))
81                     .filter_map(|(span, generics)| if generics.params.len() == 0 {
82                         Some(span)
83                     } else {
84                         None
85                     });
86                 if let Some(initial_span) = impl_spans.nth(0) {
87                     impl_spans.for_each(|additional_span| {
88                         span_lint_and_then(
89                             cx,
90                             MULTIPLE_INHERENT_IMPL,
91                             *additional_span,
92                             "Multiple implementations of this structure",
93                             |db| {
94                                 db.span_note(
95                                     *initial_span,
96                                     "First implementation here",
97                                 );
98                             },
99                         )
100                     })
101                 }
102             }
103         }
104     }
105 }