about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcsmoe <csmoe@msn.com>2023-03-29 15:12:20 +0800
committercsmoe <csmoe@msn.com>2023-03-29 08:47:32 +0000
commit4f5a019d6e836ff4b56154bf6c646cc358310fa9 (patch)
tree3d96b5bed37752105f526bb8b10bfa7c15a93c7d
parent4fdae81c70bfa1481eebc7ebec09f121c6eaf53c (diff)
downloadrust-4f5a019d6e836ff4b56154bf6c646cc358310fa9.tar.gz
rust-4f5a019d6e836ff4b56154bf6c646cc358310fa9.zip
Update clippy_lints/src/large_futures.rs
Co-authored-by: Fridtjof Stoldt <xFrednet@gmail.com>
-rw-r--r--clippy_lints/src/large_futures.rs45
-rw-r--r--tests/ui-toml/large_futures/large_futures.fixed29
-rw-r--r--tests/ui-toml/large_futures/large_futures.rs2
-rw-r--r--tests/ui-toml/large_futures/large_futures.stderr2
-rw-r--r--tests/ui/large_futures.fixed41
-rw-r--r--tests/ui/large_futures.rs24
-rw-r--r--tests/ui/large_futures.stderr56
7 files changed, 93 insertions, 106 deletions
diff --git a/clippy_lints/src/large_futures.rs b/clippy_lints/src/large_futures.rs
index 494bb2a97d2..1b054481371 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 1238c512b0f..00000000000
--- 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 80039d9047b..4158df8b5ff 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 f7895f8eaf7..b92734de2f0 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 9d839998afd..00000000000
--- 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<Output = ()>) {
-    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<Output = ()> {
-    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 8b7aaa61b88..4a8ba995da5 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<Output = ()> {
     }
 }
 
+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 557455299a9..67e0fceff6e 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