mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-22 02:00:00 +03:00
large_futures: Delay macro check
This commit is contained in:
@@ -54,29 +54,26 @@ pub fn new(future_size_threshold: u64) -> Self {
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for LargeFuture {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
if matches!(expr.span.ctxt().outer_expn_data().kind, rustc_span::ExpnKind::Macro(..)) {
|
||||
return;
|
||||
}
|
||||
if let ExprKind::Match(expr, _, MatchSource::AwaitDesugar) = expr.kind {
|
||||
if let ExprKind::Call(func, [expr, ..]) = expr.kind
|
||||
&& let ExprKind::Path(QPath::LangItem(LangItem::IntoFutureIntoFuture, ..)) = func.kind
|
||||
&& let ty = cx.typeck_results().expr_ty(expr)
|
||||
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
|
||||
&& implements_trait(cx, ty, future_trait_def_id, &[])
|
||||
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty))
|
||||
&& let size = layout.layout.size()
|
||||
&& size >= Size::from_bytes(self.future_size_threshold)
|
||||
{
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
LARGE_FUTURES,
|
||||
expr.span,
|
||||
format!("large future with a size of {} bytes", size.bytes()),
|
||||
"consider `Box::pin` on it",
|
||||
format!("Box::pin({})", snippet(cx, expr.span, "..")),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
}
|
||||
if let ExprKind::Match(scrutinee, _, MatchSource::AwaitDesugar) = expr.kind
|
||||
&& let ExprKind::Call(func, [arg, ..]) = scrutinee.kind
|
||||
&& let ExprKind::Path(QPath::LangItem(LangItem::IntoFutureIntoFuture, ..)) = func.kind
|
||||
&& !expr.span.from_expansion()
|
||||
&& let ty = cx.typeck_results().expr_ty(arg)
|
||||
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
|
||||
&& implements_trait(cx, ty, future_trait_def_id, &[])
|
||||
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty))
|
||||
&& let size = layout.layout.size()
|
||||
&& size >= Size::from_bytes(self.future_size_threshold)
|
||||
{
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
LARGE_FUTURES,
|
||||
arg.span,
|
||||
format!("large future with a size of {} bytes", size.bytes()),
|
||||
"consider `Box::pin` on it",
|
||||
format!("Box::pin({})", snippet(cx, arg.span, "..")),
|
||||
Applicability::Unspecified,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user