]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/use_self.rs
Merge branch 'macro-use' into HEAD
[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 if_chain::if_chain;
4 use rustc::hir::*;
5 use rustc::hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
6 use crate::utils::{in_macro, span_lint_and_then};
7 use syntax::ast::NodeId;
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 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
55     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
56         if in_macro(item.span) {
57             return;
58         }
59         if_chain! {
60             if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
61             if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
62             then {
63                 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
64                 let should_check = if let Some(ref params) = *parameters {
65                     !params.parenthesized && !params.args.iter().any(|arg| match arg {
66                         GenericArg::Lifetime(_) => true,
67                         GenericArg::Type(_) => false,
68                     })
69                 } else {
70                     true
71                 };
72                 if should_check {
73                     let visitor = &mut UseSelfVisitor {
74                         item_path,
75                         cx,
76                     };
77                     for impl_item_ref in refs {
78                         visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
79                     }
80                 }
81             }
82         }
83     }
84 }
85
86 struct UseSelfVisitor<'a, 'tcx: 'a> {
87     item_path: &'a Path,
88     cx: &'a LateContext<'a, 'tcx>,
89 }
90
91 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
92     fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
93         if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
94             span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
95                 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
96             });
97         }
98
99         walk_path(self, path);
100     }
101
102     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
103         NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
104     }
105 }