1 use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
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;
8 /// **What it does:** Checks for unnecessary repetition of structure name when a
9 /// replacement with `Self` is applicable.
11 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct name
12 /// feels inconsistent.
14 /// **Known problems:** None.
29 /// fn new() -> Self {
37 "Unnecessary structure name repetition whereas `Self` is applicable"
40 #[derive(Copy, Clone, Default)]
43 impl LintPass for UseSelf {
44 fn get_lints(&self) -> LintArray {
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
50 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
52 let ItemImpl(.., ref item_type, ref refs) = item.node,
53 let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
55 let visitor = &mut UseSelfVisitor {
59 for impl_item_ref in refs {
60 visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
66 struct UseSelfVisitor<'a, 'tcx: 'a> {
68 cx: &'a LateContext<'a, 'tcx>,
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 &&
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",
85 walk_path(self, path);
88 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
89 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)