|
| 1 | +use if_chain::if_chain; |
| 2 | +use rustc::declare_lint_pass; |
| 3 | +use rustc::hir::*; |
| 4 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 5 | +use rustc_session::declare_tool_lint; |
| 6 | + |
| 7 | +use crate::utils::span_lint; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// **What it does:** Checks for lifetime annotations on return values |
| 11 | + /// which might not always get bound. |
| 12 | + /// |
| 13 | + /// **Why is this bad?** If the function contains unsafe code which |
| 14 | + /// transmutes to the lifetime, the resulting lifetime may live longer |
| 15 | + /// than was intended. |
| 16 | + /// |
| 17 | + /// **Known problems*: None. |
| 18 | + /// |
| 19 | + /// **Example:** |
| 20 | + /// ```rust |
| 21 | + /// struct WrappedStr(str); |
| 22 | + /// |
| 23 | + /// // Bad: unbound return lifetime causing unsoundnes |
| 24 | + /// fn foo<'a>(x: impl AsRef<str> + 'a) -> &'a WrappedStr { |
| 25 | + /// let s = x.as_ref(); |
| 26 | + /// unsafe { &*(s as *const str as *const WrappedStr) } |
| 27 | + /// } |
| 28 | + /// |
| 29 | + /// // Good: bound return lifetime is sound |
| 30 | + /// fn foo<'a>(x: &'a str) -> &'a WrappedStr { |
| 31 | + /// unsafe { &*(x as *const str as *const WrappedStr) } |
| 32 | + /// } |
| 33 | + /// ``` |
| 34 | + pub UNBOUND_RETURN_LIFETIMES, |
| 35 | + correctness, |
| 36 | + "unbound lifetimes in function return values" |
| 37 | +} |
| 38 | + |
| 39 | +declare_lint_pass!(UnboundReturnLifetimes => [UNBOUND_RETURN_LIFETIMES]); |
| 40 | + |
| 41 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnboundReturnLifetimes { |
| 42 | + fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { |
| 43 | + if let ItemKind::Fn(ref sig, ref generics, _id) = item.kind { |
| 44 | + check_fn_inner(cx, &sig.decl, generics, None); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) { |
| 49 | + if let ImplItemKind::Method(ref sig, _) = item.kind { |
| 50 | + let parent_generics = impl_generics(cx, item.hir_id); |
| 51 | + check_fn_inner(cx, &sig.decl, &item.generics, parent_generics); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn check_fn_inner<'a, 'tcx>( |
| 57 | + cx: &LateContext<'a, 'tcx>, |
| 58 | + decl: &'tcx FnDecl, |
| 59 | + generics: &'tcx Generics, |
| 60 | + parent_generics: Option<(&'tcx Generics, Option<&'tcx Generics>, Option<&'tcx Generics>)>, |
| 61 | +) { |
| 62 | + if let FunctionRetTy::Return(ref typ) = decl.output { |
| 63 | + if let TyKind::Rptr(ref lifetime, _) = typ.kind { |
| 64 | + if let LifetimeName::Param(_) = lifetime.name { |
| 65 | + let target_lifetime = lifetime; |
| 66 | + |
| 67 | + // check function generics |
| 68 | + // the target lifetime parameter must appear on the left of some outlives relation |
| 69 | + if let Some(param) = generics.get_named(target_lifetime.name.ident().name) { |
| 70 | + if param |
| 71 | + .bounds |
| 72 | + .iter() |
| 73 | + .any(|b| if let GenericBound::Outlives(_) = b { true } else { false }) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // check parent generics |
| 80 | + // the target lifetime parameter must appear on the left of some outlives relation |
| 81 | + if let Some((ref parent_generics, _, _)) = parent_generics { |
| 82 | + if let Some(param) = parent_generics.get_named(target_lifetime.name.ident().name) { |
| 83 | + if param |
| 84 | + .bounds |
| 85 | + .iter() |
| 86 | + .any(|b| if let GenericBound::Outlives(_) = b { true } else { false }) |
| 87 | + { |
| 88 | + return; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // check type generics |
| 94 | + // the target lifetime parameter must be included in the struct |
| 95 | + if let Some((_, _, Some(ref typ_generics))) = parent_generics { |
| 96 | + if typ_generics.get_named(target_lifetime.name.ident().name).is_some() { |
| 97 | + return; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // check arguments |
| 102 | + // the target lifetime parameter must be included as a lifetime of a reference |
| 103 | + for input in decl.inputs.iter() { |
| 104 | + if let TyKind::Rptr(ref lifetime, _) = input.kind { |
| 105 | + if lifetime.name == target_lifetime.name { |
| 106 | + return; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + span_lint( |
| 112 | + cx, |
| 113 | + UNBOUND_RETURN_LIFETIMES, |
| 114 | + target_lifetime.span, |
| 115 | + "lifetime is unconstrained", |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +fn impl_generics<'tcx>( |
| 123 | + cx: &LateContext<'_, 'tcx>, |
| 124 | + hir_id: HirId, |
| 125 | +) -> Option<(&'tcx Generics, Option<&'tcx Generics>, Option<&'tcx Generics>)> { |
| 126 | + let parent_impl = cx.tcx.hir().get_parent_item(hir_id); |
| 127 | + if_chain! { |
| 128 | + if parent_impl != CRATE_HIR_ID; |
| 129 | + if let Node::Item(item) = cx.tcx.hir().get(parent_impl); |
| 130 | + if let ItemKind::Impl(_, _, _, ref parent_generics, ref _trait_ref, ref ty, _) = item.kind; |
| 131 | + then { |
| 132 | + if let TyKind::Path(ref qpath) = ty.kind { |
| 133 | + if let Some(typ_def_id) = cx.tables.qpath_res(qpath, ty.hir_id).opt_def_id() { |
| 134 | + let typ_generics = cx.tcx.hir().get_generics(typ_def_id); |
| 135 | + return Some((parent_generics, None, typ_generics)) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + None |
| 141 | +} |
0 commit comments