1 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2 use clippy_utils::trait_ref_of_method;
3 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4 use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter};
5 use rustc_hir::intravisit::{
6 walk_fn_decl, walk_generic_arg, walk_generic_param, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound,
7 walk_poly_trait_ref, walk_trait_ref, walk_ty, Visitor,
9 use rustc_hir::lang_items;
10 use rustc_hir::FnRetTy::Return;
12 BareFnTy, BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, ImplItem,
13 ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, PolyTraitRef, PredicateOrigin, TraitFn,
14 TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
16 use rustc_lint::{LateContext, LateLintPass};
17 use rustc_middle::hir::nested_filter as middle_nested_filter;
18 use rustc_middle::ty::TyCtxt;
19 use rustc_session::{declare_lint_pass, declare_tool_lint};
20 use rustc_span::def_id::LocalDefId;
21 use rustc_span::source_map::Span;
22 use rustc_span::symbol::{kw, Ident, Symbol};
24 declare_clippy_lint! {
26 /// Checks for lifetime annotations which can be removed by
27 /// relying on lifetime elision.
29 /// ### Why is this bad?
30 /// The additional lifetimes make the code look more
31 /// complicated, while there is nothing out of the ordinary going on. Removing
32 /// them leads to more readable code.
34 /// ### Known problems
35 /// - We bail out if the function has a `where` clause where lifetimes
36 /// are mentioned due to potential false positives.
37 /// - Lifetime bounds such as `impl Foo + 'a` and `T: 'a` must be elided with the
38 /// placeholder notation `'_` because the fully elided notation leaves the type bound to `'static`.
42 /// // Unnecessary lifetime annotations
43 /// fn in_and_out<'a>(x: &'a u8, y: u8) -> &'a u8 {
50 /// fn elided(x: &u8, y: u8) -> &u8 {
54 #[clippy::version = "pre 1.29.0"]
55 pub NEEDLESS_LIFETIMES,
57 "using explicit lifetimes for references in function arguments when elision rules \
58 would allow omitting them"
61 declare_clippy_lint! {
63 /// Checks for lifetimes in generics that are never used
66 /// ### Why is this bad?
67 /// The additional lifetimes make the code look more
68 /// complicated, while there is nothing out of the ordinary going on. Removing
69 /// them leads to more readable code.
73 /// // unnecessary lifetimes
74 /// fn unused_lifetime<'a>(x: u8) {
81 /// fn no_lifetime(x: u8) {
85 #[clippy::version = "pre 1.29.0"]
86 pub EXTRA_UNUSED_LIFETIMES,
88 "unused lifetimes in function definitions"
91 declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
93 impl<'tcx> LateLintPass<'tcx> for Lifetimes {
94 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
95 if let ItemKind::Fn(ref sig, generics, id) = item.kind {
96 check_fn_inner(cx, sig.decl, Some(id), None, generics, item.span, true);
97 } else if let ItemKind::Impl(impl_) = item.kind {
98 if !item.span.from_expansion() {
99 report_extra_impl_lifetimes(cx, impl_);
104 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
105 if let ImplItemKind::Fn(ref sig, id) = item.kind {
106 let report_extra_lifetimes = trait_ref_of_method(cx, item.owner_id.def_id).is_none();
114 report_extra_lifetimes,
119 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
120 if let TraitItemKind::Fn(ref sig, ref body) = item.kind {
121 let (body, trait_sig) = match *body {
122 TraitFn::Required(sig) => (None, Some(sig)),
123 TraitFn::Provided(id) => (Some(id), None),
125 check_fn_inner(cx, sig.decl, body, trait_sig, item.generics, item.span, true);
130 /// The lifetime of a &-reference.
131 #[derive(PartialEq, Eq, Hash, Debug, Clone)]
138 fn check_fn_inner<'tcx>(
139 cx: &LateContext<'tcx>,
140 decl: &'tcx FnDecl<'_>,
141 body: Option<BodyId>,
142 trait_sig: Option<&[Ident]>,
143 generics: &'tcx Generics<'_>,
145 report_extra_lifetimes: bool,
147 if span.from_expansion() || has_where_lifetimes(cx, generics) {
154 .filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
157 for pred in generics.bounds_for_param(cx.tcx.hir().local_def_id(typ.hir_id)) {
158 if pred.origin == PredicateOrigin::WhereClause {
159 // has_where_lifetimes checked that this predicate contains no lifetime.
163 for bound in pred.bounds {
164 let mut visitor = RefVisitor::new(cx);
165 walk_param_bound(&mut visitor, bound);
166 if visitor.lts.iter().any(|lt| matches!(lt, RefLt::Named(_))) {
169 if let GenericBound::Trait(ref trait_ref, _) = *bound {
170 let params = &trait_ref
175 .expect("a path must have at least one segment")
177 if let Some(params) = *params {
178 let lifetimes = params.args.iter().filter_map(|arg| match arg {
179 GenericArg::Lifetime(lt) => Some(lt),
182 for bound in lifetimes {
183 if !bound.is_static() && !bound.is_elided() {
193 if let Some(elidable_lts) = could_use_elision(cx, decl, body, trait_sig, generics.params) {
194 let lts = elidable_lts
196 // In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a
197 // `Node::GenericParam`.
198 .filter_map(|&(def_id, _)| cx.tcx.hir().get_by_def_id(def_id).ident())
199 .map(|ident| ident.to_string())
206 span.with_hi(decl.output.span().hi()),
207 &format!("the following explicit lifetimes could be elided: {lts}"),
209 if let Some(span) = elidable_lts.iter().find_map(|&(_, span)| span) {
210 diag.span_help(span, "replace with `'_` in generic arguments such as here");
216 if report_extra_lifetimes {
217 self::report_extra_lifetimes(cx, decl, generics);
221 // elision doesn't work for explicit self types, see rust-lang/rust#69064
222 fn explicit_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident: Option<Ident>) -> bool {
224 if let Some(ident) = ident;
225 if ident.name == kw::SelfLower;
226 if !func.implicit_self.has_implicit_self();
228 if let Some(self_ty) = func.inputs.first();
230 let mut visitor = RefVisitor::new(cx);
231 visitor.visit_ty(self_ty);
233 !visitor.all_lts().is_empty()
240 fn could_use_elision<'tcx>(
241 cx: &LateContext<'tcx>,
242 func: &'tcx FnDecl<'_>,
243 body: Option<BodyId>,
244 trait_sig: Option<&[Ident]>,
245 named_generics: &'tcx [GenericParam<'_>],
246 ) -> Option<Vec<(LocalDefId, Option<Span>)>> {
247 // There are two scenarios where elision works:
248 // * no output references, all input references have different LT
249 // * output references, exactly one input reference with same LT
250 // All lifetimes must be unnamed, 'static or defined without bounds on the
251 // level of the current item.
254 let allowed_lts = allowed_lts_from(cx.tcx, named_generics);
256 // these will collect all the lifetimes for references in arg/return types
257 let mut input_visitor = RefVisitor::new(cx);
258 let mut output_visitor = RefVisitor::new(cx);
260 // extract lifetimes in input argument types
261 for arg in func.inputs {
262 input_visitor.visit_ty(arg);
264 // extract lifetimes in output type
265 if let Return(ty) = func.output {
266 output_visitor.visit_ty(ty);
268 for lt in named_generics {
269 input_visitor.visit_generic_param(lt);
272 if input_visitor.abort() || output_visitor.abort() {
276 let input_lts = input_visitor.lts;
277 let output_lts = output_visitor.lts;
279 if let Some(trait_sig) = trait_sig {
280 if explicit_self_type(cx, func, trait_sig.first().copied()) {
285 if let Some(body_id) = body {
286 let body = cx.tcx.hir().body(body_id);
288 let first_ident = body.params.first().and_then(|param| param.pat.simple_ident());
289 if explicit_self_type(cx, func, first_ident) {
293 let mut checker = BodyLifetimeChecker {
294 lifetimes_used_in_body: false,
296 checker.visit_expr(body.value);
297 if checker.lifetimes_used_in_body {
302 // check for lifetimes from higher scopes
303 for lt in input_lts.iter().chain(output_lts.iter()) {
304 if !allowed_lts.contains(lt) {
309 // check for higher-ranked trait bounds
310 if !input_visitor.nested_elision_site_lts.is_empty() || !output_visitor.nested_elision_site_lts.is_empty() {
311 let allowed_lts: FxHashSet<_> = allowed_lts
313 .filter_map(|lt| match lt {
314 RefLt::Named(def_id) => Some(cx.tcx.item_name(def_id.to_def_id())),
318 for lt in input_visitor.nested_elision_site_lts {
319 if let RefLt::Named(def_id) = lt {
320 if allowed_lts.contains(&cx.tcx.item_name(def_id.to_def_id())) {
325 for lt in output_visitor.nested_elision_site_lts {
326 if let RefLt::Named(def_id) = lt {
327 if allowed_lts.contains(&cx.tcx.item_name(def_id.to_def_id())) {
334 // A lifetime can be newly elided if:
335 // - It occurs only once among the inputs.
336 // - If there are multiple input lifetimes, then the newly elided lifetime does not occur among the
337 // outputs (because eliding such an lifetime would create an ambiguity).
338 let elidable_lts = named_lifetime_occurrences(&input_lts)
340 .filter_map(|(def_id, occurrences)| {
341 if occurrences == 1 && (input_lts.len() == 1 || !output_lts.contains(&RefLt::Named(def_id))) {
345 .lifetime_generic_arg_spans
347 .or_else(|| output_visitor.lifetime_generic_arg_spans.get(&def_id))
354 .collect::<Vec<_>>();
356 if elidable_lts.is_empty() {
363 fn allowed_lts_from(tcx: TyCtxt<'_>, named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> {
364 let mut allowed_lts = FxHashSet::default();
365 for par in named_generics.iter() {
366 if let GenericParamKind::Lifetime { .. } = par.kind {
367 allowed_lts.insert(RefLt::Named(tcx.hir().local_def_id(par.hir_id)));
370 allowed_lts.insert(RefLt::Unnamed);
371 allowed_lts.insert(RefLt::Static);
375 /// Number of times each named lifetime occurs in the given slice. Returns a vector to preserve
378 fn named_lifetime_occurrences(lts: &[RefLt]) -> Vec<(LocalDefId, usize)> {
379 let mut occurrences = Vec::new();
381 if let &RefLt::Named(curr_def_id) = lt {
382 if let Some(pair) = occurrences
384 .find(|(prev_def_id, _)| *prev_def_id == curr_def_id)
388 occurrences.push((curr_def_id, 1));
395 /// A visitor usable for `rustc_front::visit::walk_ty()`.
396 struct RefVisitor<'a, 'tcx> {
397 cx: &'a LateContext<'tcx>,
399 lifetime_generic_arg_spans: FxHashMap<LocalDefId, Span>,
400 nested_elision_site_lts: Vec<RefLt>,
401 unelided_trait_object_lifetime: bool,
404 impl<'a, 'tcx> RefVisitor<'a, 'tcx> {
405 fn new(cx: &'a LateContext<'tcx>) -> Self {
409 lifetime_generic_arg_spans: FxHashMap::default(),
410 nested_elision_site_lts: Vec::new(),
411 unelided_trait_object_lifetime: false,
415 fn record(&mut self, lifetime: &Option<Lifetime>) {
416 if let Some(ref lt) = *lifetime {
418 self.lts.push(RefLt::Static);
419 } else if lt.is_anonymous() {
420 // Fresh lifetimes generated should be ignored.
421 self.lts.push(RefLt::Unnamed);
422 } else if let LifetimeName::Param(def_id) = lt.res {
423 self.lts.push(RefLt::Named(def_id));
426 self.lts.push(RefLt::Unnamed);
430 fn all_lts(&self) -> Vec<RefLt> {
433 .chain(self.nested_elision_site_lts.iter())
438 fn abort(&self) -> bool {
439 self.unelided_trait_object_lifetime
443 impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
444 // for lifetimes as parameters of generics
445 fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
446 self.record(&Some(*lifetime));
449 fn visit_poly_trait_ref(&mut self, poly_tref: &'tcx PolyTraitRef<'tcx>) {
450 let trait_ref = &poly_tref.trait_ref;
451 if let Some(id) = trait_ref.trait_def_id() && lang_items::FN_TRAITS.iter().any(|&item| {
452 self.cx.tcx.lang_items().get(item) == Some(id)
454 let mut sub_visitor = RefVisitor::new(self.cx);
455 sub_visitor.visit_trait_ref(trait_ref);
456 self.nested_elision_site_lts.append(&mut sub_visitor.all_lts());
458 walk_poly_trait_ref(self, poly_tref);
462 fn visit_ty(&mut self, ty: &'tcx Ty<'_>) {
464 TyKind::OpaqueDef(item, bounds, _) => {
465 let map = self.cx.tcx.hir();
466 let item = map.item(item);
467 let len = self.lts.len();
468 walk_item(self, item);
469 self.lts.truncate(len);
470 self.lts.extend(bounds.iter().filter_map(|bound| match bound {
471 GenericArg::Lifetime(l) => Some(if let LifetimeName::Param(def_id) = l.res {
479 TyKind::BareFn(&BareFnTy { decl, .. }) => {
480 let mut sub_visitor = RefVisitor::new(self.cx);
481 sub_visitor.visit_fn_decl(decl);
482 self.nested_elision_site_lts.append(&mut sub_visitor.all_lts());
484 TyKind::TraitObject(bounds, lt, _) => {
486 self.unelided_trait_object_lifetime = true;
488 for bound in bounds {
489 self.visit_poly_trait_ref(bound);
492 _ => walk_ty(self, ty),
496 fn visit_generic_arg(&mut self, generic_arg: &'tcx GenericArg<'tcx>) {
497 if let GenericArg::Lifetime(l) = generic_arg && let LifetimeName::Param(def_id) = l.res {
498 self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.ident.span);
500 walk_generic_arg(self, generic_arg);
504 /// Are any lifetimes mentioned in the `where` clause? If so, we don't try to
505 /// reason about elision.
506 fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_>) -> bool {
507 for predicate in generics.predicates {
509 WherePredicate::RegionPredicate(..) => return true,
510 WherePredicate::BoundPredicate(ref pred) => {
511 // a predicate like F: Trait or F: for<'a> Trait<'a>
512 let mut visitor = RefVisitor::new(cx);
513 // walk the type F, it may not contain LT refs
514 walk_ty(&mut visitor, pred.bounded_ty);
515 if !visitor.all_lts().is_empty() {
518 // if the bounds define new lifetimes, they are fine to occur
519 let allowed_lts = allowed_lts_from(cx.tcx, pred.bound_generic_params);
520 // now walk the bounds
521 for bound in pred.bounds.iter() {
522 walk_param_bound(&mut visitor, bound);
524 // and check that all lifetimes are allowed
525 if visitor.all_lts().iter().any(|it| !allowed_lts.contains(it)) {
529 WherePredicate::EqPredicate(ref pred) => {
530 let mut visitor = RefVisitor::new(cx);
531 walk_ty(&mut visitor, pred.lhs_ty);
532 walk_ty(&mut visitor, pred.rhs_ty);
533 if !visitor.lts.is_empty() {
542 struct LifetimeChecker<'cx, 'tcx, F> {
543 cx: &'cx LateContext<'tcx>,
544 map: FxHashMap<Symbol, Span>,
545 phantom: std::marker::PhantomData<F>,
548 impl<'cx, 'tcx, F> LifetimeChecker<'cx, 'tcx, F> {
549 fn new(cx: &'cx LateContext<'tcx>, map: FxHashMap<Symbol, Span>) -> LifetimeChecker<'cx, 'tcx, F> {
553 phantom: std::marker::PhantomData,
558 impl<'cx, 'tcx, F> Visitor<'tcx> for LifetimeChecker<'cx, 'tcx, F>
560 F: NestedFilter<'tcx>,
562 type Map = rustc_middle::hir::map::Map<'tcx>;
563 type NestedFilter = F;
565 // for lifetimes as parameters of generics
566 fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
567 self.map.remove(&lifetime.ident.name);
570 fn visit_generic_param(&mut self, param: &'tcx GenericParam<'_>) {
571 // don't actually visit `<'a>` or `<'a: 'b>`
572 // we've already visited the `'a` declarations and
573 // don't want to spuriously remove them
574 // `'b` in `'a: 'b` is useless unless used elsewhere in
575 // a non-lifetime bound
576 if let GenericParamKind::Type { .. } = param.kind {
577 walk_generic_param(self, param);
581 fn nested_visit_map(&mut self) -> Self::Map {
586 fn report_extra_lifetimes<'tcx>(cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>, generics: &'tcx Generics<'_>) {
590 .filter_map(|par| match par.kind {
591 GenericParamKind::Lifetime {
592 kind: LifetimeParamKind::Explicit,
593 } => Some((par.name.ident().name, par.span)),
597 let mut checker = LifetimeChecker::<hir_nested_filter::None>::new(cx, hs);
599 walk_generics(&mut checker, generics);
600 walk_fn_decl(&mut checker, func);
602 for &v in checker.map.values() {
605 EXTRA_UNUSED_LIFETIMES,
607 "this lifetime isn't used in the function definition",
612 fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<'_>) {
617 .filter_map(|par| match par.kind {
618 GenericParamKind::Lifetime {
619 kind: LifetimeParamKind::Explicit,
620 } => Some((par.name.ident().name, par.span)),
624 let mut checker = LifetimeChecker::<middle_nested_filter::All>::new(cx, hs);
626 walk_generics(&mut checker, impl_.generics);
627 if let Some(ref trait_ref) = impl_.of_trait {
628 walk_trait_ref(&mut checker, trait_ref);
630 walk_ty(&mut checker, impl_.self_ty);
631 for item in impl_.items {
632 walk_impl_item_ref(&mut checker, item);
635 for &v in checker.map.values() {
636 span_lint(cx, EXTRA_UNUSED_LIFETIMES, v, "this lifetime isn't used in the impl");
640 struct BodyLifetimeChecker {
641 lifetimes_used_in_body: bool,
644 impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
645 // for lifetimes as parameters of generics
646 fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) {
647 if !lifetime.is_anonymous() && lifetime.ident.name != kw::StaticLifetime {
648 self.lifetimes_used_in_body = true;