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.
10 use crate::utils::{in_macro, span_lint_and_sugg};
11 use if_chain::if_chain;
12 use rustc::hir::intravisit::{walk_path, walk_ty, NestedVisitorMap, Visitor};
14 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use rustc::{declare_tool_lint, lint_array};
17 use rustc_errors::Applicability;
18 use syntax::ast::NodeId;
19 use syntax_pos::symbol::keywords::SelfUpper;
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",
73 Applicability::MachineApplicable,
77 struct TraitImplTyVisitor<'a, 'tcx: 'a> {
78 item_type: ty::Ty<'tcx>,
79 cx: &'a LateContext<'a, 'tcx>,
80 trait_type_walker: ty::walk::TypeWalker<'tcx>,
81 impl_type_walker: ty::walk::TypeWalker<'tcx>,
84 impl<'a, 'tcx> Visitor<'tcx> for TraitImplTyVisitor<'a, 'tcx> {
85 fn visit_ty(&mut self, t: &'tcx Ty) {
86 let trait_ty = self.trait_type_walker.next();
87 let impl_ty = self.impl_type_walker.next();
89 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 {
93 if let Some(impl_ty) = impl_ty {
94 if self.item_type == impl_ty {
95 let is_self_ty = if let def::Def::SelfTy(..) = path.def {
102 span_use_self_lint(self.cx, path);
112 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
113 NestedVisitorMap::None
117 fn check_trait_method_impl_decl<'a, 'tcx: 'a>(
118 cx: &'a LateContext<'a, 'tcx>,
119 item_type: ty::Ty<'tcx>,
120 impl_item: &ImplItem,
121 impl_decl: &'tcx FnDecl,
122 impl_trait_ref: &ty::TraitRef<'_>,
124 let trait_method = cx
126 .associated_items(impl_trait_ref.def_id)
128 assoc_item.kind == ty::AssociatedKind::Method
131 .hygienic_eq(impl_item.ident, assoc_item.ident, impl_trait_ref.def_id)
133 .expect("impl method matches a trait method");
135 let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
136 let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
138 let impl_method_def_id = cx.tcx.hir().local_def_id(impl_item.id);
139 let impl_method_sig = cx.tcx.fn_sig(impl_method_def_id);
140 let impl_method_sig = cx.tcx.erase_late_bound_regions(&impl_method_sig);
142 let output_ty = if let FunctionRetTy::Return(ty) = &impl_decl.output {
148 // `impl_decl_ty` (of type `hir::Ty`) represents the type declared in the signature.
149 // `impl_ty` (of type `ty:TyS`) is the concrete type that the compiler has determined for
150 // that declaration. We use `impl_decl_ty` to see if the type was declared as `Self`
151 // and use `impl_ty` to check its concrete type.
152 for (impl_decl_ty, (impl_ty, trait_ty)) in impl_decl.inputs.iter().chain(output_ty).zip(
156 .zip(trait_method_sig.inputs_and_output),
158 let mut visitor = TraitImplTyVisitor {
161 trait_type_walker: trait_ty.walk(),
162 impl_type_walker: impl_ty.walk(),
165 visitor.visit_ty(&impl_decl_ty);
169 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
170 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
171 if in_macro(item.span) {
175 if let ItemKind::Impl(.., ref item_type, ref refs) = item.node;
176 if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.node;
178 let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
179 let should_check = if let Some(ref params) = *parameters {
180 !params.parenthesized && !params.args.iter().any(|arg| match arg {
181 GenericArg::Lifetime(_) => true,
182 GenericArg::Type(_) => false,
189 let visitor = &mut UseSelfVisitor {
193 let impl_def_id = cx.tcx.hir().local_def_id(item.id);
194 let impl_trait_ref = cx.tcx.impl_trait_ref(impl_def_id);
196 if let Some(impl_trait_ref) = impl_trait_ref {
197 for impl_item_ref in refs {
198 let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
199 if let ImplItemKind::Method(MethodSig{ decl: impl_decl, .. }, impl_body_id)
201 let item_type = cx.tcx.type_of(impl_def_id);
202 check_trait_method_impl_decl(cx, item_type, impl_item, impl_decl, &impl_trait_ref);
204 let body = cx.tcx.hir().body(*impl_body_id);
205 visitor.visit_body(body);
207 visitor.visit_impl_item(impl_item);
211 for impl_item_ref in refs {
212 let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
213 visitor.visit_impl_item(impl_item);
222 struct UseSelfVisitor<'a, 'tcx: 'a> {
224 cx: &'a LateContext<'a, 'tcx>,
227 impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
228 fn visit_path(&mut self, path: &'tcx Path, _id: HirId) {
229 if self.item_path.def == path.def && path.segments.last().expect(SEGMENTS_MSG).ident.name != SelfUpper.name() {
230 span_use_self_lint(self.cx, path);
233 walk_path(self, path);
236 fn visit_use(&mut self, _path: &'tcx Path, _id: NodeId, _hir_id: HirId) {
237 // Don't check use statements
240 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
241 NestedVisitorMap::All(&self.cx.tcx.hir())