Update clippy_lints/src/large_futures.rs
Co-authored-by: Fridtjof Stoldt <xFrednet@gmail.com>
This commit is contained in:
parent
4fdae81c70
commit
4f5a019d6e
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
// run-rustfix
|
||||
|
||||
#![warn(clippy::large_futures)]
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -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())`
|
||||
|
|
|
@ -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() {}
|
|
@ -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() {}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue