]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_self.rs
rustup https://github.com/rust-lang/rust/pull/68944
[rust.git] / clippy_lints / src / unused_self.rs
1 use if_chain::if_chain;
2 use rustc::hir::map::Map;
3 use rustc_hir::def::Res;
4 use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
5 use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8
9 use crate::utils::span_lint_and_help;
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks methods that contain a `self` argument but don't use it
13     ///
14     /// **Why is this bad?** It may be clearer to define the method as an associated function instead
15     /// of an instance method if it doesn't require `self`.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```rust,ignore
21     /// struct A;
22     /// impl A {
23     ///     fn method(&self) {}
24     /// }
25     /// ```
26     ///
27     /// Could be written:
28     ///
29     /// ```rust,ignore
30     /// struct A;
31     /// impl A {
32     ///     fn method() {}
33     /// }
34     /// ```
35     pub UNUSED_SELF,
36     pedantic,
37     "methods that contain a `self` argument but don't use it"
38 }
39
40 declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
41
42 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
43     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) {
44         if impl_item.span.from_expansion() {
45             return;
46         }
47         let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
48         let item = cx.tcx.hir().expect_item(parent);
49         if let ItemKind::Impl {
50             of_trait: None,
51             items: impl_item_refs,
52             ..
53         } = item.kind
54         {
55             for impl_item_ref in impl_item_refs {
56                 if_chain! {
57                     if let ImplItemRef {
58                         kind: AssocItemKind::Method { has_self: true },
59                         ..
60                     } = impl_item_ref;
61                     if let ImplItemKind::Method(_, body_id) = &impl_item.kind;
62                     let body = cx.tcx.hir().body(*body_id);
63                     if !body.params.is_empty();
64                     then {
65                         let self_param = &body.params[0];
66                         let self_hir_id = self_param.pat.hir_id;
67                         let mut visitor = UnusedSelfVisitor {
68                             cx,
69                             uses_self: false,
70                             self_hir_id: &self_hir_id,
71                         };
72                         visitor.visit_body(body);
73                         if !visitor.uses_self {
74                             span_lint_and_help(
75                                 cx,
76                                 UNUSED_SELF,
77                                 self_param.span,
78                                 "unused `self` argument",
79                                 "consider refactoring to a associated function",
80                             );
81                             return;
82                         }
83                     }
84                 }
85             }
86         };
87     }
88 }
89
90 struct UnusedSelfVisitor<'a, 'tcx> {
91     cx: &'a LateContext<'a, 'tcx>,
92     uses_self: bool,
93     self_hir_id: &'a HirId,
94 }
95
96 impl<'a, 'tcx> Visitor<'tcx> for UnusedSelfVisitor<'a, 'tcx> {
97     type Map = Map<'tcx>;
98
99     fn visit_path(&mut self, path: &'tcx Path<'_>, _id: HirId) {
100         if self.uses_self {
101             // This function already uses `self`
102             return;
103         }
104         if let Res::Local(hir_id) = &path.res {
105             self.uses_self = self.self_hir_id == hir_id
106         }
107         walk_path(self, path);
108     }
109
110     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
111         NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
112     }
113 }