1 use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
3 use rustc::hir::intravisit::{Visitor, walk_path, NestedVisitorMap};
4 use utils::{span_lint_and_then, in_macro};
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) {
51 if in_macro(item.span) {
55 let ItemImpl(.., ref item_type, ref refs) = item.node,
56 let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
58 let visitor = &mut UseSelfVisitor {
62 for impl_item_ref in refs {
63 visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
69 struct UseSelfVisitor<'a, 'tcx: 'a> {
71 cx: &'a LateContext<'a, 'tcx>,
74 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
75 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
76 if self.item_path.def == path.def &&
79 .expect("segments should be composed of at least 1 element")
80 .name != SelfType.name() {
81 span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
82 db.span_suggestion(path.span,
83 "use the applicable keyword",
88 walk_path(self, path);
91 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
92 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)