|
| 1 | +use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass}; |
| 2 | +use rustc::hir::*; |
| 3 | +use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap}; |
| 4 | +use utils::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 name |
| 12 | +/// feels inconsistent. |
| 13 | +/// |
| 14 | +/// **Known problems:** None. |
| 15 | +/// |
| 16 | +/// **Example:** |
| 17 | +/// ```rust |
| 18 | +/// struct Foo {} |
| 19 | +/// impl Foo { |
| 20 | +/// fn new() -> Foo { |
| 21 | +/// Foo {} |
| 22 | +/// } |
| 23 | +/// } |
| 24 | +/// ``` |
| 25 | +/// could be |
| 26 | +/// ``` |
| 27 | +/// struct Foo {} |
| 28 | +/// impl Foo { |
| 29 | +/// fn new() -> Self { |
| 30 | +/// Self {} |
| 31 | +/// } |
| 32 | +/// } |
| 33 | +/// ``` |
| 34 | +declare_lint! { |
| 35 | + pub USE_SELF, |
| 36 | + Allow, |
| 37 | + "Unnecessary structure name repetition whereas `Self` is applicable" |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Copy, Clone, Default)] |
| 41 | +pub struct UseSelf; |
| 42 | + |
| 43 | +impl LintPass for UseSelf { |
| 44 | + fn get_lints(&self) -> LintArray { |
| 45 | + lint_array!(USE_SELF) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf { |
| 50 | + fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { |
| 51 | + if_let_chain!([ |
| 52 | + let ItemImpl(.., ref item_type, ref refs) = item.node, |
| 53 | + let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node, |
| 54 | + ], { |
| 55 | + let visitor = &mut UseSelfVisitor { |
| 56 | + item_path: item_path, |
| 57 | + cx: cx, |
| 58 | + }; |
| 59 | + for impl_item_ref in refs { |
| 60 | + visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id)); |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +struct UseSelfVisitor<'a, 'tcx: 'a> { |
| 67 | + item_path: &'a Path, |
| 68 | + cx: &'a LateContext<'a, 'tcx>, |
| 69 | +} |
| 70 | + |
| 71 | +impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> { |
| 72 | + fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) { |
| 73 | + if self.item_path.def == path.def && |
| 74 | + path.segments |
| 75 | + .last() |
| 76 | + .expect("segments should be composed of at least 1 element") |
| 77 | + .name != SelfType.name() { |
| 78 | + span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| { |
| 79 | + db.span_suggestion(path.span, |
| 80 | + "use the applicable keyword", |
| 81 | + "Self".to_owned()); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + walk_path(self, path); |
| 86 | + } |
| 87 | + |
| 88 | + fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { |
| 89 | + NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir) |
| 90 | + } |
| 91 | +} |
0 commit comments