]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/use_self.rs
82dc0fd2b8ad0d4ab1bdcf41046a3a3327f3b867
[rust.git] / clippy_lints / src / use_self.rs
1 use crate::utils::{in_macro, span_lint_and_then};
2 use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::ty;
6 use syntax::ast::NodeId;
7 use syntax::symbol::keywords;
8 use syntax_pos::symbol::keywords::SelfType;
9
10 /// **What it does:** Checks for unnecessary repetition of structure name when a
11 /// replacement with `Self` is applicable.
12 ///
13 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
14 /// name
15 /// feels inconsistent.
16 ///
17 /// **Known problems:** None.
18 ///
19 /// **Example:**
20 /// ```rust
21 /// struct Foo {}
22 /// impl Foo {
23 ///     fn new() -> Foo {
24 ///         Foo {}
25 ///     }
26 /// }
27 /// ```
28 /// could be
29 /// ```
30 /// struct Foo {}
31 /// impl Foo {
32 ///     fn new() -> Self {
33 ///         Self {}
34 ///     }
35 /// }
36 /// ```
37 declare_clippy_lint! {
38     pub USE_SELF,
39     pedantic,
40     "Unnecessary structure name repetition whereas `Self` is applicable"
41 }
42
43 #[derive(Copy, Clone, Default)]
44 pub struct UseSelf;
45
46 impl LintPass for UseSelf {
47     fn get_lints(&self) -> LintArray {
48         lint_array!(USE_SELF)
49     }
50 }
51
52 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
53
54 fn span_use_self_lint(cx: &LateContext, path: &Path) {
55     span_lint_and_then(cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
56         db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
57     });
58 }
59
60 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
61     cx: &'a LateContext<'a, 'tcx>,
62     type_walker: ty::walk::TypeWalker<'tcx>,
63 }
64
65 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
66     fn visit_ty(&mut self, t: &'tcx Ty) {
67         let trait_ty = self.type_walker.next();
68         if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
69             let impl_is_self_ty = if let def::Def::SelfTy(..) = path.def {
70                 true
71             } else {
72                 false
73             };
74             if !impl_is_self_ty {
75                 let trait_is_self_ty = if let Some(ty::TyParam(ty::ParamTy { name, .. })) = trait_ty.map(|ty| &ty.sty) {
76                     *name == keywords::SelfType.name().as_str()
77                 } else {
78                     false
79                 };
80                 if trait_is_self_ty {
81                     span_use_self_lint(self.cx, path);
82                 }
83             }
84         }
85         walk_ty(self, t)
86     }
87
88     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
89         NestedVisitorMap::None
90     }
91 }
92
93 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
94     cx: &'a LateContext<'a, 'tcx>,
95     impl_item: &ImplItem,
96     impl_decl: &'tcx FnDecl,
97     impl_trait_ref: &ty::TraitRef,
98 ) {
99     let trait_method = cx
100         .tcx
101         .associated_items(impl_trait_ref.def_id)
102         .find(|assoc_item| {
103             assoc_item.kind == ty::AssociatedKind::Method
104                 && cx
105                     .tcx
106                     .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
107         })
108         .expect("impl method matches a trait method");
109
110     let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
111     let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
112
113     let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
114         Some(&**ty)
115     } else {
116         None
117     };
118
119     for (impl_ty, trait_ty) in impl_decl
120         .inputs
121         .iter()
122         .chain(output_ty)
123         .zip(trait_method_sig.inputs_and_output)
124     {
125         let mut visitor = TraitImplTyVisitor {
126             cx,
127             type_walker: trait_ty.walk(),
128         };
129
130         visitor.visit_ty(&impl_ty);
131     }
132 }
133
134 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
135     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
136         if in_macro(item.span) {
137             return;
138         }
139         if_chain! {
140             if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
141             if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
142             then {
143                 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
144                 let should_check = if let Some(ref params) = *parameters {
145                     !params.parenthesized && !params.args.iter().any(|arg| match arg {
146                         GenericArg::Lifetime(_) => true,
147                         GenericArg::Type(_) => false,
148                     })
149                 } else {
150                     true
151                 };
152
153                 if should_check {
154                     let visitor = &mut UseSelfVisitor {
155                         item_path,
156                         cx,
157                     };
158                     let impl_def_id = cx.tcx.hir.local_def_id(item.id);
159                     let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
160
161                     if let Some(impl_trait_ref) = impl_trait_ref {
162                         for impl_item_ref in refs {
163                             let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
164                             if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
165                                     = &impl_item.node {
166                                 check_trait_method_impl_decl(cx, impl_item, impl_decl, &impl_trait_ref);
167                                 let body = cx.tcx.hir.body(*impl_body_id);
168                                 visitor.visit_body(body);
169                             } else {
170                                 visitor.visit_impl_item(impl_item);
171                             }
172                         }
173                     } else {
174                         for impl_item_ref in refs {
175                             let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
176                             visitor.visit_impl_item(impl_item);
177                         }
178                     }
179                 }
180             }
181         }
182     }
183 }
184
185 struct UseSelfVisitor<'a, 'tcx: 'a> {
186     item_path: &'a Path,
187     cx: &'a LateContext<'a, 'tcx>,
188 }
189
190 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
191     fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
192         if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
193             span_use_self_lint(self.cx, path);
194         }
195
196         walk_path(self, path);
197     }
198
199     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
200         NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
201     }
202 }