1 use crate::utils::{in_macro, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_lint, lint_array};
8 use syntax::ast::NodeId;
9 use syntax_pos::symbol::keywords::SelfType;
11 /// **What it does:** Checks for unnecessary repetition of structure name when a
12 /// replacement with `Self` is applicable.
14 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
16 /// feels inconsistent.
18 /// **Known problems:** None.
33 /// fn new() -> Self {
38 declare_clippy_lint! {
41 "Unnecessary structure name repetition whereas `Self` is applicable"
44 #[derive(Copy, Clone, Default)]
47 impl LintPass for UseSelf {
48 fn get_lints(&self) -> LintArray {
53 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
55 fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
60 "unnecessary structure name repetition",
61 "use the applicable keyword",
66 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
68 cx: &'a LateContext<'a, 'tcx>,
69 trait_type_walker: ty::walk::TypeWalker<'tcx>,
70 impl_type_walker: ty::walk::TypeWalker<'tcx>,
73 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
74 fn visit_ty(&mut self, t: &'tcx Ty) {
75 let trait_ty = self.trait_type_walker.next();
76 let impl_ty = self.impl_type_walker.next();
78 if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
79 if self.item_path.def == path.def {
80 let is_self_ty = if let def::Def::SelfTy(..) = path.def {
86 if !is_self_ty && impl_ty != trait_ty {
87 // The implementation and trait types don't match which means that
88 // the concrete type was specified by the implementation but
89 // it didn't use `Self`
90 span_use_self_lint(self.cx, path);
97 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
98 NestedVisitorMap::None
102 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
103 cx: &'a LateContext<'a, 'tcx>,
105 impl_item: &ImplItem,
106 impl_decl: &'tcx FnDecl,
107 impl_trait_ref: &ty::TraitRef<'_>,
109 let trait_method = cx
111 .associated_items(impl_trait_ref.def_id)
113 assoc_item.kind == ty::AssociatedKind::Method
116 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
118 .expect("impl method matches a trait method");
120 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
121 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
123 let impl_method_def_id = cx.tcx.hir.local_def_id(impl_item.id);
124 let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
125 let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
127 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
133 // `impl_decl_ty` (of type `hir::Ty`) represents the type declared in the signature.
134 // `impl_ty` (of type `ty:TyS`) is the concrete type that the compiler has determined for
135 // that declaration. We use `impl_decl_ty` to see if the type was declared as `Self`
136 // and use `impl_ty` to check its concrete type.
137 for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
141 .zip(trait_method_sig.inputs_and_output),
143 let mut visitor = TraitImplTyVisitor {
146 trait_type_walker: trait_ty.walk(),
147 impl_type_walker: impl_ty.walk(),
150 visitor.visit_ty(&impl_decl_ty);
154 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
155 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
156 if in_macro(item.span) {
160 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
161 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
163 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
164 let should_check = if let Some(ref params) = *parameters {
165 !params.parenthesized && !params.args.iter().any(|arg| match arg {
166 GenericArg::Lifetime(_) => true,
167 GenericArg::Type(_) => false,
174 let visitor = &mut UseSelfVisitor {
178 let impl_def_id = cx.tcx.hir.local_def_id(item.id);
179 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
181 if let Some(impl_trait_ref) = impl_trait_ref {
182 for impl_item_ref in refs {
183 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
184 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
186 check_trait_method_impl_decl(cx, item_path, impl_item, impl_decl, &impl_trait_ref);
187 let body = cx.tcx.hir.body(*impl_body_id);
188 visitor.visit_body(body);
190 visitor.visit_impl_item(impl_item);
194 for impl_item_ref in refs {
195 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
196 visitor.visit_impl_item(impl_item);
205 struct UseSelfVisitor<'a, 'tcx: 'a> {
207 cx: &'a LateContext<'a, 'tcx>,
210 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
211 fn visit_path(&mut self, path: &'tcx Path, _id: NodeId) {
212 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
213 span_use_self_lint(self.cx, path);
216 walk_path(self, path);
219 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
220 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)