Skip to content

Commit 4fdae81

Browse files
committed
add large future lint
1 parent ba7fd68 commit 4fdae81

13 files changed

+292
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Released 2022-12-15
278278
[#9490](https://github.com/rust-lang/rust-clippy/pull/9490)
279279
* [`almost_complete_letter_range`]: No longer lints in external macros
280280
[#9467](https://github.com/rust-lang/rust-clippy/pull/9467)
281-
* [`drop_copy`]: No longer lints on idiomatic cases in match arms
281+
* [`drop_copy`]: No longer lints on idiomatic cases in match arms
282282
[#9491](https://github.com/rust-lang/rust-clippy/pull/9491)
283283
* [`question_mark`]: No longer lints in const context
284284
[#9487](https://github.com/rust-lang/rust-clippy/pull/9487)
@@ -4485,6 +4485,7 @@ Released 2018-09-13
44854485
[`large_const_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_const_arrays
44864486
[`large_digit_groups`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_digit_groups
44874487
[`large_enum_variant`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
4488+
[`large_futures`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_futures
44884489
[`large_include_file`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_include_file
44894490
[`large_stack_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_arrays
44904491
[`large_types_passed_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_types_passed_by_value

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
216216
crate::iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR_INFO,
217217
crate::large_const_arrays::LARGE_CONST_ARRAYS_INFO,
218218
crate::large_enum_variant::LARGE_ENUM_VARIANT_INFO,
219+
crate::large_futures::LARGE_FUTURES_INFO,
219220
crate::large_include_file::LARGE_INCLUDE_FILE_INFO,
220221
crate::large_stack_arrays::LARGE_STACK_ARRAYS_INFO,
221222
crate::len_zero::COMPARISON_TO_EMPTY_INFO,

clippy_lints/src/large_futures.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 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+
}
87+
}
88+
}
89+
}
90+
}

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ mod items_after_statements;
161161
mod iter_not_returning_iterator;
162162
mod large_const_arrays;
163163
mod large_enum_variant;
164+
mod large_futures;
164165
mod large_include_file;
165166
mod large_stack_arrays;
166167
mod len_zero;
@@ -800,6 +801,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
800801
store.register_late_pass(move |_| Box::new(dereference::Dereferencing::new(msrv())));
801802
store.register_late_pass(|_| Box::new(option_if_let_else::OptionIfLetElse));
802803
store.register_late_pass(|_| Box::new(future_not_send::FutureNotSend));
804+
let future_size_threshold = conf.future_size_threshold;
805+
store.register_late_pass(move |_| Box::new(large_futures::LargeFuture::new(future_size_threshold)));
803806
store.register_late_pass(|_| Box::new(if_let_mutex::IfLetMutex));
804807
store.register_late_pass(|_| Box::new(if_not_else::IfNotElse));
805808
store.register_late_pass(|_| Box::new(equatable_if_let::PatternEquality));

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ define_Conf! {
459459
/// Whether to **only** check for missing documentation in items visible within the current
460460
/// crate. For example, `pub(crate)` items.
461461
(missing_docs_in_crate_items: bool = false),
462+
/// Lint: LARGE_FUTURES.
463+
///
464+
/// The maximum byte size a `Future` can have, before it triggers the `clippy::large_futures` lint
465+
(future_size_threshold: u64 = 16 * 1024),
462466
}
463467

464468
/// Search for the configuration file.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
future-size-threshold = 1024
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::large_futures)]
4+
5+
fn main() {}
6+
7+
pub async fn should_warn() {
8+
let x = [0u8; 1024];
9+
async {}.await;
10+
dbg!(x);
11+
}
12+
13+
pub async fn should_not_warn() {
14+
let x = [0u8; 1020];
15+
async {}.await;
16+
dbg!(x);
17+
}
18+
19+
pub async fn bar() {
20+
Box::pin(should_warn()).await;
21+
22+
async {
23+
let x = [0u8; 1024];
24+
dbg!(x);
25+
}
26+
.await;
27+
28+
should_not_warn().await;
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::large_futures)]
4+
5+
fn main() {}
6+
7+
pub async fn should_warn() {
8+
let x = [0u8; 1024];
9+
async {}.await;
10+
dbg!(x);
11+
}
12+
13+
pub async fn should_not_warn() {
14+
let x = [0u8; 1020];
15+
async {}.await;
16+
dbg!(x);
17+
}
18+
19+
pub async fn bar() {
20+
should_warn().await;
21+
22+
async {
23+
let x = [0u8; 1024];
24+
dbg!(x);
25+
}
26+
.await;
27+
28+
should_not_warn().await;
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: large future with a size of 1026 bytes
2+
--> $DIR/large_futures.rs:20:5
3+
|
4+
LL | should_warn().await;
5+
| ^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(should_warn())`
6+
|
7+
= note: `-D clippy::large-futures` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
2424
enforced-import-renames
2525
enum-variant-name-threshold
2626
enum-variant-size-threshold
27+
future-size-threshold
2728
ignore-interior-mutability
2829
large-error-threshold
2930
literal-representation-threshold

tests/ui/large_futures.fixed

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// run-rustfix
2+
3+
#![feature(generators)]
4+
#![warn(clippy::large_futures)]
5+
#![allow(clippy::future_not_send)]
6+
#![allow(clippy::manual_async_fn)]
7+
8+
async fn big_fut(_arg: [u8; 1024 * 16]) {}
9+
10+
async fn wait() {
11+
let f = async {
12+
Box::pin(big_fut([0u8; 1024 * 16])).await;
13+
};
14+
Box::pin(f).await
15+
}
16+
async fn calls_fut(fut: impl std::future::Future<Output = ()>) {
17+
loop {
18+
Box::pin(wait()).await;
19+
if true {
20+
return fut.await;
21+
} else {
22+
Box::pin(wait()).await;
23+
}
24+
}
25+
}
26+
27+
pub async fn test() {
28+
let fut = big_fut([0u8; 1024 * 16]);
29+
Box::pin(foo()).await;
30+
Box::pin(calls_fut(fut)).await;
31+
}
32+
33+
pub fn foo() -> impl std::future::Future<Output = ()> {
34+
async {
35+
let x = [0i32; 1024 * 16];
36+
async {}.await;
37+
dbg!(x);
38+
}
39+
}
40+
41+
fn main() {}

tests/ui/large_futures.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// run-rustfix
2+
3+
#![feature(generators)]
4+
#![warn(clippy::large_futures)]
5+
#![allow(clippy::future_not_send)]
6+
#![allow(clippy::manual_async_fn)]
7+
8+
async fn big_fut(_arg: [u8; 1024 * 16]) {}
9+
10+
async fn wait() {
11+
let f = async {
12+
big_fut([0u8; 1024 * 16]).await;
13+
};
14+
f.await
15+
}
16+
async fn calls_fut(fut: impl std::future::Future<Output = ()>) {
17+
loop {
18+
wait().await;
19+
if true {
20+
return fut.await;
21+
} else {
22+
wait().await;
23+
}
24+
}
25+
}
26+
27+
pub async fn test() {
28+
let fut = big_fut([0u8; 1024 * 16]);
29+
foo().await;
30+
calls_fut(fut).await;
31+
}
32+
33+
pub fn foo() -> impl std::future::Future<Output = ()> {
34+
async {
35+
let x = [0i32; 1024 * 16];
36+
async {}.await;
37+
dbg!(x);
38+
}
39+
}
40+
41+
fn main() {}

tests/ui/large_futures.stderr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: large future with a size of 16385 bytes
2+
--> $DIR/large_futures.rs:12:9
3+
|
4+
LL | big_fut([0u8; 1024 * 16]).await;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(big_fut([0u8; 1024 * 16]))`
6+
|
7+
= note: `-D clippy::large-futures` implied by `-D warnings`
8+
9+
error: large future with a size of 16386 bytes
10+
--> $DIR/large_futures.rs:14:5
11+
|
12+
LL | f.await
13+
| ^ help: consider `Box::pin` on it: `Box::pin(f)`
14+
15+
error: large future with a size of 16387 bytes
16+
--> $DIR/large_futures.rs:18:9
17+
|
18+
LL | wait().await;
19+
| ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())`
20+
21+
error: large future with a size of 16387 bytes
22+
--> $DIR/large_futures.rs:22:13
23+
|
24+
LL | wait().await;
25+
| ^^^^^^ help: consider `Box::pin` on it: `Box::pin(wait())`
26+
27+
error: large future with a size of 65540 bytes
28+
--> $DIR/large_futures.rs:29:5
29+
|
30+
LL | foo().await;
31+
| ^^^^^ help: consider `Box::pin` on it: `Box::pin(foo())`
32+
33+
error: large future with a size of 49159 bytes
34+
--> $DIR/large_futures.rs:30:5
35+
|
36+
LL | calls_fut(fut).await;
37+
| ^^^^^^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(calls_fut(fut))`
38+
39+
error: aborting due to 6 previous errors
40+

0 commit comments

Comments
 (0)