|
1 | 1 | use if_chain::if_chain;
|
2 | 2 | use rustc_hir::def::Res;
|
3 | 3 | use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
|
4 |
| -use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path}; |
| 4 | +use rustc_hir::{HirId, ImplItem, ImplItemKind, ItemKind, Path}; |
5 | 5 | use rustc_lint::{LateContext, LateLintPass};
|
6 | 6 | use rustc_middle::hir::map::Map;
|
7 | 7 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
@@ -45,45 +45,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
|
45 | 45 | return;
|
46 | 46 | }
|
47 | 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::Fn(_, 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 |
| - } |
| 48 | + let parent_item = cx.tcx.hir().expect_item(parent); |
| 49 | + let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id); |
| 50 | + let assoc_item = cx.tcx.associated_item(def_id); |
| 51 | + if_chain! { |
| 52 | + if let ItemKind::Impl { of_trait: None, .. } = parent_item.kind; |
| 53 | + if assoc_item.method_has_self_argument; |
| 54 | + if let ImplItemKind::Fn(.., body_id) = &impl_item.kind; |
| 55 | + let body = cx.tcx.hir().body(*body_id); |
| 56 | + if !body.params.is_empty(); |
| 57 | + then { |
| 58 | + let self_param = &body.params[0]; |
| 59 | + let self_hir_id = self_param.pat.hir_id; |
| 60 | + let mut visitor = UnusedSelfVisitor { |
| 61 | + cx, |
| 62 | + uses_self: false, |
| 63 | + self_hir_id: &self_hir_id, |
| 64 | + }; |
| 65 | + visitor.visit_body(body); |
| 66 | + if !visitor.uses_self { |
| 67 | + span_lint_and_help( |
| 68 | + cx, |
| 69 | + UNUSED_SELF, |
| 70 | + self_param.span, |
| 71 | + "unused `self` argument", |
| 72 | + "consider refactoring to a associated function", |
| 73 | + ); |
| 74 | + return; |
84 | 75 | }
|
85 | 76 | }
|
86 |
| - }; |
| 77 | + } |
87 | 78 | }
|
88 | 79 | }
|
89 | 80 |
|
|
0 commit comments