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_tool_lint, lint_array};
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) {
59 "unnecessary structure name repetition",
60 "use the applicable keyword",
65 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
67 cx: &'a LateContext<'a, 'tcx>,
68 trait_type_walker: ty::walk::TypeWalker<'tcx>,
69 impl_type_walker: ty::walk::TypeWalker<'tcx>,
72 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
73 fn visit_ty(&mut self, t: &'tcx Ty) {
74 let trait_ty = self.trait_type_walker.next();
75 let impl_ty = self.impl_type_walker.next();
77 if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
78 if self.item_path.def == path.def {
79 let is_self_ty = if let def::Def::SelfTy(..) = path.def {
85 if !is_self_ty && impl_ty != trait_ty {
86 // The implementation and trait types don't match which means that
87 // the concrete type was specified by the implementation but
88 // it didn't use `Self`
89 span_use_self_lint(self.cx, path);
96 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
97 NestedVisitorMap::None
101 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
102 cx: &'a LateContext<'a, 'tcx>,
104 impl_item: &ImplItem,
105 impl_decl: &'tcx FnDecl,
106 impl_trait_ref: &ty::TraitRef<'_>,
108 let trait_method = cx
110 .associated_items(impl_trait_ref.def_id)
112 assoc_item.kind == ty::AssociatedKind::Method
115 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
117 .expect("impl method matches a trait method");
119 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
120 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
122 let impl_method_def_id = cx.tcx.hir.local_def_id(impl_item.id);
123 let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
124 let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
126 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
132 // `impl_decl_ty` (of type `hir::Ty`) represents the type declared in the signature.
133 // `impl_ty` (of type `ty:TyS`) is the concrete type that the compiler has determined for
134 // that declaration. We use `impl_decl_ty` to see if the type was declared as `Self`
135 // and use `impl_ty` to check its concrete type.
136 for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
140 .zip(trait_method_sig.inputs_and_output),
142 let mut visitor = TraitImplTyVisitor {
145 trait_type_walker: trait_ty.walk(),
146 impl_type_walker: impl_ty.walk(),
149 visitor.visit_ty(&impl_decl_ty);
153 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
154 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
155 if in_macro(item.span) {
159 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
160 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
162 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
163 let should_check = if let Some(ref params) = *parameters {
164 !params.parenthesized && !params.args.iter().any(|arg| match arg {
165 GenericArg::Lifetime(_) => true,
166 GenericArg::Type(_) => false,
173 let visitor = &mut UseSelfVisitor {
177 let impl_def_id = cx.tcx.hir.local_def_id(item.id);
178 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
180 if let Some(impl_trait_ref) = impl_trait_ref {
181 for impl_item_ref in refs {
182 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
183 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
185 check_trait_method_impl_decl(cx, item_path, impl_item, impl_decl, &impl_trait_ref);
186 let body = cx.tcx.hir.body(*impl_body_id);
187 visitor.visit_body(body);
189 visitor.visit_impl_item(impl_item);
193 for impl_item_ref in refs {
194 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
195 visitor.visit_impl_item(impl_item);
204 struct UseSelfVisitor<'a, 'tcx: 'a> {
206 cx: &'a LateContext<'a, 'tcx>,
209 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
210 fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
211 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
212 span_use_self_lint(self.cx, path);
215 walk_path(self, path);
218 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
219 NestedVisitorMap::OnlyBodies(&self.cx.tcx.hir)