1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
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;
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
13 /// feels inconsistent.
15 /// **Known problems:** None.
30 /// fn new() -> Self {
38 "Unnecessary structure name repetition whereas `Self` is applicable"
41 #[derive(Copy, Clone, Default)]
44 impl LintPass for UseSelf {
45 fn get_lints(&self) -> LintArray {
50 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
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) {
58 if let ItemImpl(.., ref item_type, ref refs) = item.node;
59 if let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node;
61 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters;
62 let should_check = if let Some(ref params) = *parameters {
63 !params.parenthesized && params.lifetimes.len() == 0
68 let visitor = &mut UseSelfVisitor {
72 for impl_item_ref in refs {
73 visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
81 struct UseSelfVisitor<'a, 'tcx: 'a> {
83 cx: &'a LateContext<'a, 'tcx>,
86 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
87 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
88 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).name != SelfType.name() {
89 span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
90 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
94 walk_path(self, path);
97 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
98 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)