]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/inherent_impl.rs
Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
[rust.git] / clippy_lints / src / inherent_impl.rs
1 //! lint on inherent implementations
2
3 use crate::utils::{in_macro, span_lint_and_then};
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_hir::{def_id, Crate, Impl, Item, ItemKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
8 use rustc_span::Span;
9
10 declare_clippy_lint! {
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     pub MULTIPLE_INHERENT_IMPL,
38     restriction,
39     "Multiple inherent impl that could be grouped"
40 }
41
42 #[allow(clippy::module_name_repetitions)]
43 #[derive(Default)]
44 pub struct MultipleInherentImpl {
45     impls: FxHashMap<def_id::DefId, Span>,
46 }
47
48 impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
49
50 impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
51     fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
52         if let ItemKind::Impl(Impl {
53             ref generics,
54             of_trait: None,
55             ..
56         }) = item.kind
57         {
58             // Remember for each inherent implementation encountered its span and generics
59             // but filter out implementations that have generic params (type or lifetime)
60             // or are derived from a macro
61             if !in_macro(item.span) && generics.params.is_empty() {
62                 self.impls.insert(item.def_id.to_def_id(), item.span);
63             }
64         }
65     }
66
67     fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'_>) {
68         if !krate.items.is_empty() {
69             // Retrieve all inherent implementations from the crate, grouped by type
70             for impls in cx.tcx.crate_inherent_impls(def_id::LOCAL_CRATE).inherent_impls.values() {
71                 // Filter out implementations that have generic params (type or lifetime)
72                 let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def));
73                 if let Some(initial_span) = impl_spans.next() {
74                     impl_spans.for_each(|additional_span| {
75                         span_lint_and_then(
76                             cx,
77                             MULTIPLE_INHERENT_IMPL,
78                             *additional_span,
79                             "multiple implementations of this structure",
80                             |diag| {
81                                 diag.span_note(*initial_span, "first implementation here");
82                             },
83                         )
84                     })
85                 }
86             }
87         }
88     }
89 }