1 use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
3 use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
5 use syntax::ast::NodeId;
7 /// **What it does:** Checks for unnecessary repetition of structure name when a
8 /// replacement with `Self` is applicable.
10 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct name
11 /// feels inconsistent.
13 /// **Known problems:** None.
28 /// fn new() -> Self {
36 "Repetitive struct name usage whereas `Self` is applicable"
39 #[derive(Copy, Clone, Default)]
42 impl LintPass for UseSelf {
43 fn get_lints(&self) -> LintArray {
48 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
49 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
51 let ItemImpl(.., ref item_type, ref refs) = item.node,
52 let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
54 let visitor = &mut UseSelfVisitor {
58 for impl_item_ref in refs {
59 visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
65 struct UseSelfVisitor<'a, 'tcx: 'a> {
67 cx: &'a LateContext<'a, 'tcx>,
70 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
71 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
72 if self.item_path.def == path.def &&
75 .expect("segments should be composed of at least 1 elemnt")
81 "repetitive struct name usage. Use `Self` instead.");
84 walk_path(self, path);
87 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
88 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)