1 use crate::utils::{in_macro, span_lint_and_then};
2 use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use syntax::ast::NodeId;
7 use syntax::symbol::keywords;
8 use syntax_pos::symbol::keywords::SelfType;
10 /// **What it does:** Checks for unnecessary repetition of structure name when a
11 /// replacement with `Self` is applicable.
13 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
15 /// feels inconsistent.
17 /// **Known problems:** None.
32 /// fn new() -> Self {
37 declare_clippy_lint! {
40 "Unnecessary structure name repetition whereas `Self` is applicable"
43 #[derive(Copy, Clone, Default)]
46 impl LintPass for UseSelf {
47 fn get_lints(&self) -> LintArray {
52 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
54 fn span_use_self_lint(cx: &LateContext, path: &Path) {
55 span_lint_and_then(cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
56 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
60 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
61 cx: &'a LateContext<'a, 'tcx>,
62 type_walker: ty::walk::TypeWalker<'tcx>,
65 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
66 fn visit_ty(&mut self, t: &'tcx Ty) {
67 let trait_ty = self.type_walker.next();
68 if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
69 let impl_is_self_ty = if let def::Def::SelfTy(..) = path.def {
75 let trait_is_self_ty = if let Some(ty::TyParam(ty::ParamTy { name, .. })) = trait_ty.map(|ty| &ty.sty) {
76 *name == keywords::SelfType.name().as_str()
81 span_use_self_lint(self.cx, path);
88 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
89 NestedVisitorMap::None
93 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
94 cx: &'a LateContext<'a, 'tcx>,
96 impl_decl: &'tcx FnDecl,
97 impl_trait_ref: &ty::TraitRef,
101 .associated_items(impl_trait_ref.def_id)
103 assoc_item.kind == ty::AssociatedKind::Method
106 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
108 .expect("impl method matches a trait method");
110 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
111 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
113 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
119 for (impl_ty, trait_ty) in impl_decl
123 .zip(trait_method_sig.inputs_and_output)
125 let mut visitor = TraitImplTyVisitor {
127 type_walker: trait_ty.walk(),
130 visitor.visit_ty(&impl_ty);
134 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
135 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
136 if in_macro(item.span) {
140 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
141 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
143 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
144 let should_check = if let Some(ref params) = *parameters {
145 !params.parenthesized && !params.args.iter().any(|arg| match arg {
146 GenericArg::Lifetime(_) => true,
147 GenericArg::Type(_) => false,
154 let visitor = &mut UseSelfVisitor {
158 let impl_def_id = cx.tcx.hir.local_def_id(item.id);
159 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
161 if let Some(impl_trait_ref) = impl_trait_ref {
162 for impl_item_ref in refs {
163 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
164 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
166 check_trait_method_impl_decl(cx, impl_item, impl_decl, &impl_trait_ref);
167 let body = cx.tcx.hir.body(*impl_body_id);
168 visitor.visit_body(body);
170 visitor.visit_impl_item(impl_item);
174 for impl_item_ref in refs {
175 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
176 visitor.visit_impl_item(impl_item);
185 struct UseSelfVisitor<'a, 'tcx: 'a> {
187 cx: &'a LateContext<'a, 'tcx>,
190 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
191 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
192 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
193 span_use_self_lint(self.cx, path);
196 walk_path(self, path);
199 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
200 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)