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 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
52 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
53 if in_macro(item.span) {
57 let ItemImpl(.., ref item_type, ref refs) = item.node,
58 let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node,
59 let PathParameters::AngleBracketedParameters(ref param_data)
60 = item_path.segments.last().expect(SEGMENTS_MSG).parameters,
61 param_data.lifetimes.len() == 0,
63 let visitor = &mut UseSelfVisitor {
67 for impl_item_ref in refs {
68 visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id));
74 struct UseSelfVisitor<'a, 'tcx: 'a> {
76 cx: &'a LateContext<'a, 'tcx>,
79 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
80 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
81 if self.item_path.def == path.def &&
85 .name != SelfType.name() {
86 span_lint_and_then(self.cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
87 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
91 walk_path(self, path);
94 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
95 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)