|
| 1 | +use rustc_hir::def_id::LocalDefId; |
| 2 | +use rustc_hir::intravisit::{walk_fn_decl, FnKind, Visitor}; |
| 3 | +use rustc_hir::{ |
| 4 | + Body, FnDecl, FnSig, ImplItem, ImplItemKind, Lifetime, LifetimeName, TraitItem, TraitItemKind, |
| 5 | +}; |
| 6 | +use rustc_middle::ty::layout::HasTyCtxt; |
| 7 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 8 | +use rustc_span::symbol::kw; |
| 9 | +use rustc_span::Span; |
| 10 | + |
| 11 | +use crate::lints::{ElidedNamedLifetime, ElidedNamedLifetimeSuggestion}; |
| 12 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 13 | + |
| 14 | +declare_lint! { |
| 15 | + /// The `elided_named_lifetimes` lint detects when an elided |
| 16 | + /// lifetime ends up being a named lifetime, such as `'static` |
| 17 | + /// or some lifetime parameter `'a`. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// |
| 21 | + /// ```rust,compile_fail |
| 22 | + /// #![deny(elided_named_lifetimes)] |
| 23 | + /// struct Foo; |
| 24 | + /// impl Foo { |
| 25 | + /// pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 { |
| 26 | + /// unsafe { &mut *(x as *mut _) } |
| 27 | + /// } |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// |
| 31 | + /// {{produces}} |
| 32 | + /// |
| 33 | + /// ### Explanation |
| 34 | + /// |
| 35 | + /// Lifetime elision is quite useful, because it frees you from having |
| 36 | + /// to give each lifetime its own name, but sometimes it can produce |
| 37 | + /// somewhat surprising resolutions. In safe code, it is mostly okay, |
| 38 | + /// because the borrow checker prevents any unsoundness, so the worst |
| 39 | + /// case scenario is you get a confusing error message in some other place. |
| 40 | + /// But with `unsafe` code, such unexpected resolutions may lead to unsound code. |
| 41 | + pub ELIDED_NAMED_LIFETIMES, |
| 42 | + Warn, |
| 43 | + "detects when an elided lifetime gets resolved to be `'static` or some named parameter" |
| 44 | +} |
| 45 | + |
| 46 | +declare_lint_pass!(ElidedNamedLifetimes => [ELIDED_NAMED_LIFETIMES]); |
| 47 | + |
| 48 | +impl<'tcx> LateLintPass<'tcx> for ElidedNamedLifetimes { |
| 49 | + fn check_fn( |
| 50 | + &mut self, |
| 51 | + cx: &LateContext<'tcx>, |
| 52 | + _: FnKind<'tcx>, |
| 53 | + decl: &'tcx FnDecl<'tcx>, |
| 54 | + _: &'tcx Body<'tcx>, |
| 55 | + _: Span, |
| 56 | + _: LocalDefId, |
| 57 | + ) { |
| 58 | + self.check_decl(cx, decl) |
| 59 | + } |
| 60 | + |
| 61 | + fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) { |
| 62 | + if let TraitItemKind::Fn(FnSig { decl, .. }, _) = item.kind { |
| 63 | + self.check_decl(cx, decl) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { |
| 68 | + if let ImplItemKind::Fn(FnSig { decl, .. }, _) = item.kind { |
| 69 | + self.check_decl(cx, decl) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl ElidedNamedLifetimes { |
| 75 | + fn check_decl<'tcx>(&self, cx: &LateContext<'tcx>, decl: &'tcx FnDecl<'tcx>) { |
| 76 | + walk_fn_decl(&mut LifetimeVisitor { cx }, decl) |
| 77 | + } |
| 78 | +} |
| 79 | +struct LifetimeVisitor<'a, 'tcx> { |
| 80 | + cx: &'a LateContext<'tcx>, |
| 81 | +} |
| 82 | + |
| 83 | +impl<'tcx> Visitor<'tcx> for LifetimeVisitor<'_, 'tcx> { |
| 84 | + fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) -> Self::Result { |
| 85 | + // `.is_elided()` should probably be called `.resolves_to_elided()`, |
| 86 | + // and `.is_anonymous()` is actually the thing that we need here. |
| 87 | + if !lifetime.is_anonymous() { |
| 88 | + return; |
| 89 | + } |
| 90 | + let (name, declaration) = match lifetime.res { |
| 91 | + LifetimeName::Param(param) => { |
| 92 | + let tcx = self.cx.tcx(); |
| 93 | + let name = tcx.item_name(param.into()); |
| 94 | + if name == kw::UnderscoreLifetime { |
| 95 | + return; |
| 96 | + } |
| 97 | + let span = tcx.source_span(param); |
| 98 | + (name, Some(span)) |
| 99 | + } |
| 100 | + LifetimeName::Static => (kw::StaticLifetime, None), |
| 101 | + LifetimeName::ImplicitObjectLifetimeDefault |
| 102 | + | LifetimeName::Error |
| 103 | + | LifetimeName::Infer => return, |
| 104 | + }; |
| 105 | + self.cx.emit_lint( |
| 106 | + ELIDED_NAMED_LIFETIMES, |
| 107 | + ElidedNamedLifetime { |
| 108 | + span: lifetime.ident.span, |
| 109 | + sugg: { |
| 110 | + let (span, code) = lifetime.suggestion(); |
| 111 | + ElidedNamedLifetimeSuggestion { span, code } |
| 112 | + }, |
| 113 | + name, |
| 114 | + declaration, |
| 115 | + }, |
| 116 | + ) |
| 117 | + } |
| 118 | +} |
0 commit comments