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