]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/use_self.rs
Run nightly rustfmt
[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_lint! {
36     pub USE_SELF,
37     Allow,
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_let_chain!([
58             let ItemImpl(.., ref item_type, ref refs) = item.node,
59             let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
60         ], {
61             let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters;
62             if !parameters.parenthesized && parameters.lifetimes.len() == 0 {
63                 let visitor = &mut UseSelfVisitor {
64                     item_path: item_path,
65                     cx: cx,
66                 };
67                 for impl_item_ref in refs {
68                     visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
69                 }
70             }
71         })
72     }
73 }
74
75 struct UseSelfVisitor<'a, 'tcx: 'a> {
76     item_path: &'a Path,
77     cx: &'a LateContext<'a, 'tcx>,
78 }
79
80 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
81     fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
82         if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).name != SelfType.name() {
83             span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
84                 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
85             });
86         }
87
88         walk_path(self, path);
89     }
90
91     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
92         NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)
93     }
94 }