From 4f5a019d6e836ff4b56154bf6c646cc358310fa9 Mon Sep 17 00:00:00 2001 From: csmoe Date: Wed, 29 Mar 2023 15:12:20 +0800 Subject: [PATCH] Update clippy_lints/src/large_futures.rs Co-authored-by: Fridtjof Stoldt --- clippy_lints/src/large_futures.rs | 45 +++++++-------- .../ui-toml/large_futures/large_futures.fixed | 29 ---------- tests/ui-toml/large_futures/large_futures.rs | 2 - .../large_futures/large_futures.stderr | 2 +- tests/ui/large_futures.fixed | 41 -------------- tests/ui/large_futures.rs | 24 +++++++- tests/ui/large_futures.stderr | 56 ++++++++++++++++--- 7 files changed, 93 insertions(+), 106 deletions(-) delete mode 100644 tests/ui-toml/large_futures/large_futures.fixed delete mode 100644 tests/ui/large_futures.fixed diff --git a/clippy_lints/src/large_futures.rs b/clippy_lints/src/large_futures.rs index 494bb2a97d26..1b0544813718 100644 --- a/clippy_lints/src/large_futures.rs +++ b/clippy_lints/src/large_futures.rs @@ -59,31 +59,28 @@ impl_lint_pass!(LargeFuture => [LARGE_FUTURES]); 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 { - if matches!( - func.kind, - ExprKind::Path(QPath::LangItem(LangItem::IntoFutureIntoFuture, ..)) - ) { - let ty = cx.typeck_results().expr_ty(expr); - if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait() - && implements_trait(cx, ty, future_trait_def_id, &[]) { - if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty)) { - let size = layout.layout.size(); - if 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::MachineApplicable, - ); - } - } - } - } + 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, + ); } } } diff --git a/tests/ui-toml/large_futures/large_futures.fixed b/tests/ui-toml/large_futures/large_futures.fixed deleted file mode 100644 index 1238c512b0fa..000000000000 --- a/tests/ui-toml/large_futures/large_futures.fixed +++ /dev/null @@ -1,29 +0,0 @@ -// run-rustfix - -#![warn(clippy::large_futures)] - -fn main() {} - -pub async fn should_warn() { - let x = [0u8; 1024]; - async {}.await; - dbg!(x); -} - -pub async fn should_not_warn() { - let x = [0u8; 1020]; - async {}.await; - dbg!(x); -} - -pub async fn bar() { - Box::pin(should_warn()).await; - - async { - let x = [0u8; 1024]; - dbg!(x); - } - .await; - - should_not_warn().await; -} diff --git a/tests/ui-toml/large_futures/large_futures.rs b/tests/ui-toml/large_futures/large_futures.rs index 80039d9047b3..4158df8b5ff5 100644 --- a/tests/ui-toml/large_futures/large_futures.rs +++ b/tests/ui-toml/large_futures/large_futures.rs @@ -1,5 +1,3 @@ -// run-rustfix - #![warn(clippy::large_futures)] fn main() {} diff --git a/tests/ui-toml/large_futures/large_futures.stderr b/tests/ui-toml/large_futures/large_futures.stderr index f7895f8eaf7a..b92734de2f08 100644 --- a/tests/ui-toml/large_futures/large_futures.stderr +++ b/tests/ui-toml/large_futures/large_futures.stderr @@ -1,5 +1,5 @@ error: large future with a size of 1026 bytes - --> $DIR/large_futures.rs:20:5 + --> $DIR/large_futures.rs:18:5 | LL | should_warn().await; | ^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(should_warn())` diff --git a/tests/ui/large_futures.fixed b/tests/ui/large_futures.fixed deleted file mode 100644 index 9d839998afd7..000000000000 --- a/tests/ui/large_futures.fixed +++ /dev/null @@ -1,41 +0,0 @@ -// run-rustfix - -#![feature(generators)] -#![warn(clippy::large_futures)] -#![allow(clippy::future_not_send)] -#![allow(clippy::manual_async_fn)] - -async fn big_fut(_arg: [u8; 1024 * 16]) {} - -async fn wait() { - let f = async { - Box::pin(big_fut([0u8; 1024 * 16])).await; - }; - Box::pin(f).await -} -async fn calls_fut(fut: impl std::future::Future) { - loop { - Box::pin(wait()).await; - if true { - return fut.await; - } else { - Box::pin(wait()).await; - } - } -} - -pub async fn test() { - let fut = big_fut([0u8; 1024 * 16]); - Box::pin(foo()).await; - Box::pin(calls_fut(fut)).await; -} - -pub fn foo() -> impl std::future::Future { - async { - let x = [0i32; 1024 * 16]; - async {}.await; - dbg!(x); - } -} - -fn main() {} diff --git a/tests/ui/large_futures.rs b/tests/ui/large_futures.rs index 8b7aaa61b888..4a8ba995da55 100644 --- a/tests/ui/large_futures.rs +++ b/tests/ui/large_futures.rs @@ -1,5 +1,3 @@ -// run-rustfix - #![feature(generators)] #![warn(clippy::large_futures)] #![allow(clippy::future_not_send)] @@ -38,4 +36,26 @@ pub fn foo() -> impl std::future::Future { } } +pub async fn lines() { + async { + let x = [0i32; 1024 * 16]; + async {}.await; + println!("{:?}", x); + } + .await; +} + +pub async fn macro_expn() { + macro_rules! macro_ { + () => { + async { + let x = [0i32; 1024 * 16]; + async {}.await; + println!("macro: {:?}", x); + } + }; + } + macro_!().await +} + fn main() {} diff --git a/tests/ui/large_futures.stderr b/tests/ui/large_futures.stderr index 557455299a9c..67e0fceff6ef 100644 --- a/tests/ui/large_futures.stderr +++ b/tests/ui/large_futures.stderr @@ -1,5 +1,5 @@ error: large future with a size of 16385 bytes - --> $DIR/large_futures.rs:12:9 + --> $DIR/large_futures.rs:10:9 | LL | big_fut([0u8; 1024 * 16]).await; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(big_fut([0u8; 1024 * 16]))` @@ -7,34 +7,76 @@ LL | big_fut([0u8; 1024 * 16]).await; = note: `-D clippy::large-futures` implied by `-D warnings` error: large future with a size of 16386 bytes - --> $DIR/large_futures.rs:14:5 + --> $DIR/large_futures.rs:12:5 | LL | f.await | ^ help: consider `Box::pin` on it: `Box::pin(f)` error: large future with a size of 16387 bytes - --> $DIR/large_futures.rs:18:9 + --> $DIR/large_futures.rs:16:9 | LL | wait().await; | ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())` error: large future with a size of 16387 bytes - --> $DIR/large_futures.rs:22:13 + --> $DIR/large_futures.rs:20:13 | LL | wait().await; | ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())` error: large future with a size of 65540 bytes - --> $DIR/large_futures.rs:29:5 + --> $DIR/large_futures.rs:27:5 | LL | foo().await; | ^^^^^ help: consider `Box::pin` on it: `Box::pin(foo())` error: large future with a size of 49159 bytes - --> $DIR/large_futures.rs:30:5 + --> $DIR/large_futures.rs:28:5 | LL | calls_fut(fut).await; | ^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(calls_fut(fut))` -error: aborting due to 6 previous errors +error: large future with a size of 65540 bytes + --> $DIR/large_futures.rs:40:5 + | +LL | / async { +LL | | let x = [0i32; 1024 * 16]; +LL | | async {}.await; +LL | | println!("{:?}", x); +LL | | } + | |_____^ + | +help: consider `Box::pin` on it + | +LL ~ Box::pin(async { +LL + let x = [0i32; 1024 * 16]; +LL + async {}.await; +LL + println!("{:?}", x); +LL + }) + | + +error: large future with a size of 65540 bytes + --> $DIR/large_futures.rs:51:13 + | +LL | / async { +LL | | let x = [0i32; 1024 * 16]; +LL | | async {}.await; +LL | | println!("macro: {:?}", x); +LL | | } + | |_____________^ +... +LL | macro_!().await + | --------- in this macro invocation + | + = note: this error originates in the macro `macro_` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider `Box::pin` on it + | +LL ~ Box::pin(async { +LL + let x = [0i32; 1024 * 16]; +LL + async {}.await; +LL + println!("macro: {:?}", x); +LL + }) + | + +error: aborting due to 8 previous errors