]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/use_self.rs
Fix ICE for issues 2767, 2499, 1782
[rust.git] / clippy_lints / src / use_self.rs
1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::hir::*;
3 use rustc::hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
4 use utils::{in_macro, span_lint_and_then};
5 use syntax::ast::NodeId;
6 use syntax_pos::symbol::keywords::SelfType;
7
8 /// **What it does:** Checks for unnecessary repetition of structure name when a
9 /// replacement with `Self` is applicable.
10 ///
11 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
12 /// name
13 /// feels inconsistent.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:**
18 /// ```rust
19 /// struct Foo {}
20 /// impl Foo {
21 ///     fn new() -> Foo {
22 ///         Foo {}
23 ///     }
24 /// }
25 /// ```
26 /// could be
27 /// ```
28 /// struct Foo {}
29 /// impl Foo {
30 ///     fn new() -> Self {
31 ///         Self {}
32 ///     }
33 /// }
34 /// ```
35 declare_clippy_lint! {
36     pub USE_SELF,
37     pedantic,
38     "Unnecessary structure name repetition whereas `Self` is applicable"
39 }
40
41 #[derive(Copy, Clone, Default)]
42 pub struct UseSelf;
43
44 impl LintPass for UseSelf {
45     fn get_lints(&self) -> LintArray {
46         lint_array!(USE_SELF)
47     }
48 }
49
50 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
51
52 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
53     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
54         if in_macro(item.span) {
55             return;
56         }
57         if_chain! {
58             if let ItemImpl(.., ref item_type, ref refs) = item.node;
59             if let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node;
60             then {
61                 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters;
62                 let should_check = if let Some(ref params) = *parameters {
63                     !params.parenthesized && params.lifetimes.len() == 0
64                 } else {
65                     true
66                 };
67                 if should_check {
68                     let visitor = &mut UseSelfVisitor {
69                         item_path,
70                         cx,
71                     };
72                     for impl_item_ref in refs {
73                         visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
74                     }
75                 }
76             }
77         }
78     }
79 }
80
81 struct UseSelfVisitor<'a, 'tcx: 'a> {
82     item_path: &'a Path,
83     cx: &'a LateContext<'a, 'tcx>,
84 }
85
86 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
87     fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
88         if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).name != SelfType.name() {
89             span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
90                 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
91             });
92         }
93
94         walk_path(self, path);
95     }
96
97     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
98         NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
99     }
100 }