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