]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/lifetimes.rs
rustup https://github.com/rust-lang/rust/pull/68944
[rust.git] / clippy_lints / src / lifetimes.rs
index 5edf1cb839f8e1d5e87d79e8a5b210062e886b3b..a9408e369b396afdfaa806d6e4ba169c19b79fce 100644 (file)
@@ -1,15 +1,23 @@
 use matches::matches;
-use rustc::declare_lint_pass;
-use rustc::hir::def::{DefKind, Res};
-use rustc::hir::intravisit::*;
-use rustc::hir::*;
-use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
+use rustc::hir::map::Map;
+use rustc::lint::in_external_macro;
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
-use rustc_session::declare_tool_lint;
-use syntax::source_map::Span;
-use syntax::symbol::kw;
-
-use crate::reexport::*;
+use rustc_hir::def::{DefKind, Res};
+use rustc_hir::intravisit::{
+    walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_ty, NestedVisitorMap, Visitor,
+};
+use rustc_hir::FnRetTy::Return;
+use rustc_hir::{
+    BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, ImplItem, ImplItemKind, Item,
+    ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitItem, TraitItemKind, TraitMethod, Ty,
+    TyKind, WhereClause, WherePredicate,
+};
+use rustc_lint::{LateContext, LateLintPass, LintContext};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::Span;
+use rustc_span::symbol::kw;
+
+use crate::reexport::Name;
 use crate::utils::{last_path_segment, span_lint, trait_ref_of_method};
 
 declare_clippy_lint! {
@@ -92,7 +100,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem<'
     }
 
     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem<'_>) {
-        if let TraitItemKind::Method(ref sig, ref body) = item.kind {
+        if let TraitItemKind::Fn(ref sig, ref body) = item.kind {
             let body = match *body {
                 TraitMethod::Required(_) => None,
                 TraitMethod::Provided(id) => Some(id),
@@ -112,9 +120,9 @@ enum RefLt {
 
 fn check_fn_inner<'a, 'tcx>(
     cx: &LateContext<'a, 'tcx>,
-    decl: &'tcx FnDecl,
+    decl: &'tcx FnDecl<'_>,
     body: Option<BodyId>,
-    generics: &'tcx Generics,
+    generics: &'tcx Generics<'_>,
     span: Span,
     report_extra_lifetimes: bool,
 ) {
@@ -128,7 +136,7 @@ fn check_fn_inner<'a, 'tcx>(
         _ => false,
     });
     for typ in types {
-        for bound in &typ.bounds {
+        for bound in typ.bounds {
             let mut visitor = RefVisitor::new(cx);
             walk_param_bound(&mut visitor, bound);
             if visitor.lts.iter().any(|lt| matches!(lt, RefLt::Named(_))) {
@@ -173,9 +181,9 @@ fn check_fn_inner<'a, 'tcx>(
 
 fn could_use_elision<'a, 'tcx>(
     cx: &LateContext<'a, 'tcx>,
-    func: &'tcx FnDecl,
+    func: &'tcx FnDecl<'_>,
     body: Option<BodyId>,
-    named_generics: &'tcx [GenericParam],
+    named_generics: &'tcx [GenericParam<'_>],
     bounds_lts: Vec<&'tcx Lifetime>,
 ) -> bool {
     // There are two scenarios where elision works:
@@ -192,7 +200,7 @@ fn could_use_elision<'a, 'tcx>(
     let mut output_visitor = RefVisitor::new(cx);
 
     // extract lifetimes in input argument types
-    for arg in &func.inputs {
+    for arg in func.inputs {
         input_visitor.visit_ty(arg);
     }
     // extract lifetimes in output type
@@ -258,7 +266,7 @@ fn could_use_elision<'a, 'tcx>(
     }
 }
 
-fn allowed_lts_from(named_generics: &[GenericParam]) -> FxHashSet<RefLt> {
+fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> {
     let mut allowed_lts = FxHashSet::default();
     for par in named_generics.iter() {
         if let GenericParamKind::Lifetime { .. } = par.kind {
@@ -328,7 +336,7 @@ fn into_vec(self) -> Option<Vec<RefLt>> {
         }
     }
 
-    fn collect_anonymous_lifetimes(&mut self, qpath: &QPath, ty: &Ty) {
+    fn collect_anonymous_lifetimes(&mut self, qpath: &QPath<'_>, ty: &Ty<'_>) {
         if let Some(ref last_path_segment) = last_path_segment(qpath).args {
             if !last_path_segment.parenthesized
                 && !last_path_segment.args.iter().any(|arg| match arg {
@@ -358,12 +366,14 @@ fn collect_anonymous_lifetimes(&mut self, qpath: &QPath, ty: &Ty) {
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
+    type Map = Map<'tcx>;
+
     // for lifetimes as parameters of generics
     fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
         self.record(&Some(*lifetime));
     }
 
-    fn visit_ty(&mut self, ty: &'tcx Ty) {
+    fn visit_ty(&mut self, ty: &'tcx Ty<'_>) {
         match ty.kind {
             TyKind::Rptr(ref lt, _) if lt.is_elided() => {
                 self.record(&None);
@@ -374,7 +384,7 @@ fn visit_ty(&mut self, ty: &'tcx Ty) {
             TyKind::Def(item, _) => {
                 let map = self.cx.tcx.hir();
                 if let ItemKind::OpaqueTy(ref exist_ty) = map.expect_item(item.id).kind {
-                    for bound in &exist_ty.bounds {
+                    for bound in exist_ty.bounds {
                         if let GenericBound::Outlives(_) = *bound {
                             self.record(&None);
                         }
@@ -384,7 +394,7 @@ fn visit_ty(&mut self, ty: &'tcx Ty) {
                 }
                 walk_ty(self, ty);
             },
-            TyKind::TraitObject(ref bounds, ref lt) => {
+            TyKind::TraitObject(bounds, ref lt) => {
                 if !lt.is_elided() {
                     self.abort = true;
                 }
@@ -397,15 +407,15 @@ fn visit_ty(&mut self, ty: &'tcx Ty) {
         }
         walk_ty(self, ty);
     }
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
         NestedVisitorMap::None
     }
 }
 
 /// Are any lifetimes mentioned in the `where` clause? If so, we don't try to
 /// reason about elision.
-fn has_where_lifetimes<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, where_clause: &'tcx WhereClause) -> bool {
-    for predicate in &where_clause.predicates {
+fn has_where_lifetimes<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, where_clause: &'tcx WhereClause<'_>) -> bool {
+    for predicate in where_clause.predicates {
         match *predicate {
             WherePredicate::RegionPredicate(..) => return true,
             WherePredicate::BoundPredicate(ref pred) => {
@@ -452,12 +462,14 @@ struct LifetimeChecker {
 }
 
 impl<'tcx> Visitor<'tcx> for LifetimeChecker {
+    type Map = Map<'tcx>;
+
     // for lifetimes as parameters of generics
     fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
         self.map.remove(&lifetime.name.ident().name);
     }
 
-    fn visit_generic_param(&mut self, param: &'tcx GenericParam) {
+    fn visit_generic_param(&mut self, param: &'tcx GenericParam<'_>) {
         // don't actually visit `<'a>` or `<'a: 'b>`
         // we've already visited the `'a` declarations and
         // don't want to spuriously remove them
@@ -467,12 +479,12 @@ fn visit_generic_param(&mut self, param: &'tcx GenericParam) {
             walk_generic_param(self, param)
         }
     }
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
         NestedVisitorMap::None
     }
 }
 
-fn report_extra_lifetimes<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, func: &'tcx FnDecl, generics: &'tcx Generics) {
+fn report_extra_lifetimes<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, func: &'tcx FnDecl<'_>, generics: &'tcx Generics<'_>) {
     let hs = generics
         .params
         .iter()
@@ -501,6 +513,8 @@ struct BodyLifetimeChecker {
 }
 
 impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
+    type Map = Map<'tcx>;
+
     // for lifetimes as parameters of generics
     fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
         if lifetime.name.ident().name != kw::Invalid && lifetime.name.ident().name != kw::StaticLifetime {
@@ -508,7 +522,7 @@ fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
         }
     }
 
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
         NestedVisitorMap::None
     }
 }