use rustc_data_structures::fx::{FxHashMap, FxHashSet};
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,
+ walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_trait_ref, 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, TraitFn, TraitItem, TraitItemKind, Ty,
- TyKind, WhereClause, WherePredicate,
+ ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind,
+ TraitRef, Ty, TyKind, WhereClause, WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map;
use rustc_span::source_map::Span;
use rustc_span::symbol::{kw, Symbol};
-use crate::utils::{in_macro, last_path_segment, span_lint, trait_ref_of_method};
+use crate::utils::paths;
+use crate::utils::{get_trait_def_id, in_macro, last_path_segment, span_lint, trait_ref_of_method};
declare_clippy_lint! {
/// **What it does:** Checks for lifetime annotations which can be removed by
return;
}
+ // fn pointers and closure trait bounds are also lifetime elision sites. This lint does not
+ // support nested elision sites in a fn item.
+ if FnPointerOrClosureTraitBoundFinder::find_in_generics(cx, generics)
+ || FnPointerOrClosureTraitBoundFinder::find_in_fn_decl(cx, decl)
+ {
+ return;
+ }
+
let mut bounds_lts = Vec::new();
let types = generics
.params
NestedVisitorMap::None
}
}
+
+const CLOSURE_TRAIT_BOUNDS: [&[&str]; 3] = [&paths::FN, &paths::FN_MUT, &paths::FN_ONCE];
+
+struct FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
+ cx: &'a LateContext<'tcx>,
+ found: bool,
+}
+
+impl<'a, 'tcx> FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
+ fn find_in_generics(cx: &'a LateContext<'tcx>, generics: &'tcx Generics<'tcx>) -> bool {
+ let mut finder = Self { cx, found: false };
+ finder.visit_generics(generics);
+ finder.found
+ }
+
+ fn find_in_fn_decl(cx: &'a LateContext<'tcx>, generics: &'tcx FnDecl<'tcx>) -> bool {
+ let mut finder = Self { cx, found: false };
+ finder.visit_fn_decl(generics);
+ finder.found
+ }
+}
+
+impl<'a, 'tcx> Visitor<'tcx> for FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
+ type Map = Map<'tcx>;
+ fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
+ NestedVisitorMap::None
+ }
+
+ fn visit_trait_ref(&mut self, tref: &'tcx TraitRef<'tcx>) {
+ if CLOSURE_TRAIT_BOUNDS
+ .iter()
+ .any(|trait_path| tref.trait_def_id() == get_trait_def_id(self.cx, trait_path))
+ {
+ self.found = true;
+ }
+ walk_trait_ref(self, tref);
+ }
+
+ fn visit_ty(&mut self, ty: &'tcx Ty<'tcx>) {
+ match ty.kind {
+ TyKind::BareFn(..) => self.found = true,
+ TyKind::OpaqueDef(item_id, _) => {
+ let map = self.cx.tcx.hir();
+ let item = map.expect_item(item_id.id);
+ self.visit_item(item);
+ },
+ _ => (),
+ }
+ walk_ty(self, ty);
+ }
+}
pub const FMT_ARGUMENTS_NEW_V1: [&str; 4] = ["core", "fmt", "Arguments", "new_v1"];
pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments", "new_v1_formatted"];
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];
+pub const FN: [&str; 3] = ["core", "ops", "Fn"];
+pub const FN_MUT: [&str; 3] = ["core", "ops", "FnMut"];
+pub const FN_ONCE: [&str; 3] = ["core", "ops", "FnOnce"];
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];