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