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_pos::symbol::keywords::SelfType;
9 /// **What it does:** Checks for unnecessary repetition of structure name when a
10 /// replacement with `Self` is applicable.
12 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
14 /// feels inconsistent.
16 /// **Known problems:** None.
31 /// fn new() -> Self {
36 declare_clippy_lint! {
39 "Unnecessary structure name repetition whereas `Self` is applicable"
42 #[derive(Copy, Clone, Default)]
45 impl LintPass for UseSelf {
46 fn get_lints(&self) -> LintArray {
51 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
53 fn span_use_self_lint(cx: &LateContext, path: &Path) {
54 span_lint_and_then(cx, USE_SELF, path.span, "unnecessary structure name repetition", |db| {
55 db.span_suggestion(path.span, "use the applicable keyword", "Self".to_owned());
59 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
61 cx: &'a LateContext<'a, 'tcx>,
62 trait_type_walker: ty::walk::TypeWalker<'tcx>,
63 impl_type_walker: ty::walk::TypeWalker<'tcx>,
66 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
67 fn visit_ty(&mut self, t: &'tcx Ty) {
68 let trait_ty = self.trait_type_walker.next();
69 let impl_ty = self.impl_type_walker.next();
71 if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
72 if self.item_path.def == path.def {
73 let is_self_ty = if let def::Def::SelfTy(..) = path.def {
79 if !is_self_ty && impl_ty != trait_ty {
80 // The implementation and trait types don't match which means that
81 // the concrete type was specified by the implementation but
82 // it didn't use `Self`
83 span_use_self_lint(self.cx, path);
90 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
91 NestedVisitorMap::None
95 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
96 cx: &'a LateContext<'a, 'tcx>,
99 impl_decl: &'tcx FnDecl,
100 impl_trait_ref: &ty::TraitRef,
102 let trait_method = cx
104 .associated_items(impl_trait_ref.def_id)
106 assoc_item.kind == ty::AssociatedKind::Method
109 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
111 .expect("impl method matches a trait method");
113 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
114 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
116 let impl_method_def_id = cx.tcx.hir.local_def_id(impl_item.id);
117 let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
118 let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
120 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
126 for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
130 .zip(trait_method_sig.inputs_and_output),
132 let mut visitor = TraitImplTyVisitor {
135 trait_type_walker: trait_ty.walk(),
136 impl_type_walker: impl_ty.walk(),
139 visitor.visit_ty(&impl_decl_ty);
143 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
144 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
145 if in_macro(item.span) {
149 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
150 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
152 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
153 let should_check = if let Some(ref params) = *parameters {
154 !params.parenthesized && !params.args.iter().any(|arg| match arg {
155 GenericArg::Lifetime(_) => true,
156 GenericArg::Type(_) => false,
163 let visitor = &mut UseSelfVisitor {
167 let impl_def_id = cx.tcx.hir.local_def_id(item.id);
168 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
170 if let Some(impl_trait_ref) = impl_trait_ref {
171 for impl_item_ref in refs {
172 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
173 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
175 check_trait_method_impl_decl(cx, item_path, impl_item, impl_decl, &impl_trait_ref);
176 let body = cx.tcx.hir.body(*impl_body_id);
177 visitor.visit_body(body);
179 visitor.visit_impl_item(impl_item);
183 for impl_item_ref in refs {
184 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
185 visitor.visit_impl_item(impl_item);
194 struct UseSelfVisitor<'a, 'tcx: 'a> {
196 cx: &'a LateContext<'a, 'tcx>,
199 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
200 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
201 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
202 span_use_self_lint(self.cx, path);
205 walk_path(self, path);
208 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
209 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)