|
| 1 | +use clippy_utils::source::snippet; |
| 2 | +use clippy_utils::{diagnostics::span_lint_and_sugg, ty::implements_trait}; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 7 | +use rustc_target::abi::Size; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// It checks for the size of a `Future` created by `async fn` or `async {}`. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// Due to the current [unideal implemention](https://github.com/rust-lang/rust/issues/69826) of `Generator`, |
| 15 | + /// large size of a `Future` may cause stack overflows. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// async fn wait(f: impl std::future::Future<Output = ()>) {} |
| 20 | + /// |
| 21 | + /// async fn big_fut(arg: [u8; 1024]) {} |
| 22 | + /// |
| 23 | + /// pub async fn test() { |
| 24 | + /// let fut = big_fut([0u8; 1024]); |
| 25 | + /// wait(fut).await; |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// `Box::pin` the big future instead. |
| 30 | + /// |
| 31 | + /// ```rust |
| 32 | + /// async fn wait(f: impl std::future::Future<Output = ()>) {} |
| 33 | + /// |
| 34 | + /// async fn big_fut(arg: [u8; 1024]) {} |
| 35 | + /// |
| 36 | + /// pub async fn test() { |
| 37 | + /// let fut = Box::pin(big_fut([0u8; 1024])); |
| 38 | + /// wait(fut).await; |
| 39 | + /// } |
| 40 | + /// ``` |
| 41 | + #[clippy::version = "1.68.0"] |
| 42 | + pub LARGE_FUTURES, |
| 43 | + pedantic, |
| 44 | + "large future may lead to unexpected stack overflows" |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Copy, Clone)] |
| 48 | +pub struct LargeFuture { |
| 49 | + future_size_threshold: u64, |
| 50 | +} |
| 51 | + |
| 52 | +impl LargeFuture { |
| 53 | + pub fn new(future_size_threshold: u64) -> Self { |
| 54 | + Self { future_size_threshold } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl_lint_pass!(LargeFuture => [LARGE_FUTURES]); |
| 59 | + |
| 60 | +impl<'tcx> LateLintPass<'tcx> for LargeFuture { |
| 61 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 62 | + if matches!(expr.span.ctxt().outer_expn_data().kind, rustc_span::ExpnKind::Macro(..)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + if let ExprKind::Match(expr, _, MatchSource::AwaitDesugar) = expr.kind { |
| 66 | + if let ExprKind::Call(func, [expr, ..]) = expr.kind |
| 67 | + && let ExprKind::Path(QPath::LangItem(LangItem::IntoFutureIntoFuture, ..)) = func.kind |
| 68 | + && let ty = cx.typeck_results().expr_ty(expr) |
| 69 | + && let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() |
| 70 | + && implements_trait(cx, ty, future_trait_def_id, &[]) |
| 71 | + && let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty)) |
| 72 | + && let size = layout.layout.size() |
| 73 | + && size >= Size::from_bytes(self.future_size_threshold) |
| 74 | + { |
| 75 | + span_lint_and_sugg( |
| 76 | + cx, |
| 77 | + LARGE_FUTURES, |
| 78 | + expr.span, |
| 79 | + &format!("large future with a size of {} bytes", size.bytes()), |
| 80 | + "consider `Box::pin` on it", |
| 81 | + format!("Box::pin({})", snippet(cx, expr.span, "..")), |
| 82 | + Applicability::Unspecified, |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments