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