1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::utils::{in_macro, span_lint_and_sugg};
12 use if_chain::if_chain;
13 use crate::rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
14 use crate::rustc::hir::*;
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
17 use crate::rustc::{declare_tool_lint, lint_array};
18 use crate::syntax_pos::symbol::keywords::SelfType;
19 use crate::syntax::ast::NodeId;
21 /// **What it does:** Checks for unnecessary repetition of structure name when a
22 /// replacement with `Self` is applicable.
24 /// **Why is this bad?** Unnecessary repetition. Mixed use of `Self` and struct
26 /// feels inconsistent.
28 /// **Known problems:** None.
43 /// fn new() -> Self {
48 declare_clippy_lint! {
51 "Unnecessary structure name repetition whereas `Self` is applicable"
54 #[derive(Copy, Clone, Default)]
57 impl LintPass for UseSelf {
58 fn get_lints(&self) -> LintArray {
63 const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
65 fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path) {
70 "unnecessary structure name repetition",
71 "use the applicable keyword",
76 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
77 item_type: ty::Ty<'tcx>,
78 cx: &'a LateContext<'a, 'tcx>,
79 trait_type_walker: ty::walk::TypeWalker<'tcx>,
80 impl_type_walker: ty::walk::TypeWalker<'tcx>,
83 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
84 fn visit_ty(&mut self, t: &'tcx Ty) {
85 let trait_ty = self.trait_type_walker.next();
86 let impl_ty = self.impl_type_walker.next();
88 if let TyKind::Path(QPath::Resolved(_, path)) = &t.node {
90 // The implementation and trait types don't match which means that
91 // the concrete type was specified by the implementation
92 if impl_ty != trait_ty {
94 if let Some(impl_ty) = impl_ty {
95 if self.item_type == impl_ty {
96 let is_self_ty = if let def::Def::SelfTy(..) = path.def {
103 span_use_self_lint(self.cx, path);
114 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
115 NestedVisitorMap::None
119 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
120 cx: &'a LateContext<'a, 'tcx>,
121 item_type: ty::Ty<'tcx>,
122 impl_item: &ImplItem,
123 impl_decl: &'tcx FnDecl,
124 impl_trait_ref: &ty::TraitRef<'_>,
126 let trait_method = cx
128 .associated_items(impl_trait_ref.def_id)
130 assoc_item.kind == ty::AssociatedKind::Method
133 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
135 .expect("impl method matches a trait method");
137 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
138 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
140 let impl_method_def_id = cx.tcx.hir.local_def_id(impl_item.id);
141 let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
142 let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
144 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
150 // `impl_decl_ty` (of type `hir::Ty`) represents the type declared in the signature.
151 // `impl_ty` (of type `ty:TyS`) is the concrete type that the compiler has determined for
152 // that declaration. We use `impl_decl_ty` to see if the type was declared as `Self`
153 // and use `impl_ty` to check its concrete type.
154 for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
158 .zip(trait_method_sig.inputs_and_output),
160 let mut visitor = TraitImplTyVisitor {
163 trait_type_walker: trait_ty.walk(),
164 impl_type_walker: impl_ty.walk(),
167 visitor.visit_ty(&impl_decl_ty);
171 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
172 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
173 if in_macro(item.span) {
177 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
178 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
180 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
181 let should_check = if let Some(ref params) = *parameters {
182 !params.parenthesized && !params.args.iter().any(|arg| match arg {
183 GenericArg::Lifetime(_) => true,
184 GenericArg::Type(_) => false,
191 let visitor = &mut UseSelfVisitor {
195 let impl_def_id = cx.tcx.hir.local_def_id(item.id);
196 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
198 if let Some(impl_trait_ref) = impl_trait_ref {
199 for impl_item_ref in refs {
200 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
201 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
203 let item_type = cx.tcx.type_of(impl_def_id);
204 check_trait_method_impl_decl(cx, item_type, impl_item, impl_decl, &impl_trait_ref);
206 let body = cx.tcx.hir.body(*impl_body_id);
207 visitor.visit_body(body);
209 visitor.visit_impl_item(impl_item);
213 for impl_item_ref in refs {
214 let impl_item = cx.tcx.hir.impl_item(impl_item_ref.id);
215 visitor.visit_impl_item(impl_item);
224 struct UseSelfVisitor<'a, 'tcx: 'a> {
226 cx: &'a LateContext<'a, 'tcx>,
229 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
230 fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
231 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfType.name() {
232 span_use_self_lint(self.cx, path);
235 walk_path(self, path);
238 fn visit_use(&mut self, _path: &'tcx Path, _id: NodeId, _hir_id: HirId) {
239 // Don't check use statements
242 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
243 NestedVisitorMap::All(&self.cx.tcx.hir)