Skip to content

Commit 4f5a019

Browse files
csmoexFrednet
andcommitted
Update clippy_lints/src/large_futures.rs
Co-authored-by: Fridtjof Stoldt <[email protected]>
1 parent 4fdae81 commit 4f5a019

File tree

7 files changed

+93
-106
lines changed

7 files changed

+93
-106
lines changed

clippy_lints/src/large_futures.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,28 @@ impl_lint_pass!(LargeFuture => [LARGE_FUTURES]);
5959

6060
impl<'tcx> LateLintPass<'tcx> for LargeFuture {
6161
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+
}
6265
if let ExprKind::Match(expr, _, MatchSource::AwaitDesugar) = expr.kind {
63-
if let ExprKind::Call(func, [expr, ..]) = expr.kind {
64-
if matches!(
65-
func.kind,
66-
ExprKind::Path(QPath::LangItem(LangItem::IntoFutureIntoFuture, ..))
67-
) {
68-
let ty = cx.typeck_results().expr_ty(expr);
69-
if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
70-
&& implements_trait(cx, ty, future_trait_def_id, &[]) {
71-
if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty)) {
72-
let size = layout.layout.size();
73-
if size >= Size::from_bytes(self.future_size_threshold) {
74-
span_lint_and_sugg(
75-
cx,
76-
LARGE_FUTURES,
77-
expr.span,
78-
&format!("large future with a size of {} bytes", size.bytes()),
79-
"consider `Box::pin` on it",
80-
format!("Box::pin({})", snippet(cx, expr.span, "..")),
81-
Applicability::MachineApplicable,
82-
);
83-
}
84-
}
85-
}
86-
}
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+
);
8784
}
8885
}
8986
}

tests/ui-toml/large_futures/large_futures.fixed

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/ui-toml/large_futures/large_futures.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
#![warn(clippy::large_futures)]
42

53
fn main() {}

tests/ui-toml/large_futures/large_futures.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: large future with a size of 1026 bytes
2-
--> $DIR/large_futures.rs:20:5
2+
--> $DIR/large_futures.rs:18:5
33
|
44
LL | should_warn().await;
55
| ^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(should_warn())`

tests/ui/large_futures.fixed

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/ui/large_futures.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
#![feature(generators)]
42
#![warn(clippy::large_futures)]
53
#![allow(clippy::future_not_send)]
@@ -38,4 +36,26 @@ pub fn foo() -> impl std::future::Future<Output = ()> {
3836
}
3937
}
4038

39+
pub async fn lines() {
40+
async {
41+
let x = [0i32; 1024 * 16];
42+
async {}.await;
43+
println!("{:?}", x);
44+
}
45+
.await;
46+
}
47+
48+
pub async fn macro_expn() {
49+
macro_rules! macro_ {
50+
() => {
51+
async {
52+
let x = [0i32; 1024 * 16];
53+
async {}.await;
54+
println!("macro: {:?}", x);
55+
}
56+
};
57+
}
58+
macro_!().await
59+
}
60+
4161
fn main() {}

tests/ui/large_futures.stderr

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,82 @@
11
error: large future with a size of 16385 bytes
2-
--> $DIR/large_futures.rs:12:9
2+
--> $DIR/large_futures.rs:10:9
33
|
44
LL | big_fut([0u8; 1024 * 16]).await;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(big_fut([0u8; 1024 * 16]))`
66
|
77
= note: `-D clippy::large-futures` implied by `-D warnings`
88

99
error: large future with a size of 16386 bytes
10-
--> $DIR/large_futures.rs:14:5
10+
--> $DIR/large_futures.rs:12:5
1111
|
1212
LL | f.await
1313
| ^ help: consider `Box::pin` on it: `Box::pin(f)`
1414

1515
error: large future with a size of 16387 bytes
16-
--> $DIR/large_futures.rs:18:9
16+
--> $DIR/large_futures.rs:16:9
1717
|
1818
LL | wait().await;
1919
| ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())`
2020

2121
error: large future with a size of 16387 bytes
22-
--> $DIR/large_futures.rs:22:13
22+
--> $DIR/large_futures.rs:20:13
2323
|
2424
LL | wait().await;
2525
| ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())`
2626

2727
error: large future with a size of 65540 bytes
28-
--> $DIR/large_futures.rs:29:5
28+
--> $DIR/large_futures.rs:27:5
2929
|
3030
LL | foo().await;
3131
| ^^^^^ help: consider `Box::pin` on it: `Box::pin(foo())`
3232

3333
error: large future with a size of 49159 bytes
34-
--> $DIR/large_futures.rs:30:5
34+
--> $DIR/large_futures.rs:28:5
3535
|
3636
LL | calls_fut(fut).await;
3737
| ^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(calls_fut(fut))`
3838

39-
error: aborting due to 6 previous errors
39+
error: large future with a size of 65540 bytes
40+
--> $DIR/large_futures.rs:40:5
41+
|
42+
LL | / async {
43+
LL | | let x = [0i32; 1024 * 16];
44+
LL | | async {}.await;
45+
LL | | println!("{:?}", x);
46+
LL | | }
47+
| |_____^
48+
|
49+
help: consider `Box::pin` on it
50+
|
51+
LL ~ Box::pin(async {
52+
LL + let x = [0i32; 1024 * 16];
53+
LL + async {}.await;
54+
LL + println!("{:?}", x);
55+
LL + })
56+
|
57+
58+
error: large future with a size of 65540 bytes
59+
--> $DIR/large_futures.rs:51:13
60+
|
61+
LL | / async {
62+
LL | | let x = [0i32; 1024 * 16];
63+
LL | | async {}.await;
64+
LL | | println!("macro: {:?}", x);
65+
LL | | }
66+
| |_____________^
67+
...
68+
LL | macro_!().await
69+
| --------- in this macro invocation
70+
|
71+
= note: this error originates in the macro `macro_` (in Nightly builds, run with -Z macro-backtrace for more info)
72+
help: consider `Box::pin` on it
73+
|
74+
LL ~ Box::pin(async {
75+
LL + let x = [0i32; 1024 * 16];
76+
LL + async {}.await;
77+
LL + println!("macro: {:?}", x);
78+
LL + })
79+
|
80+
81+
error: aborting due to 8 previous errors
4082

0 commit comments

Comments
 (0)