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