Do not look for significant drop inside .await expansion

Temporaries created inside the expansion of `.await` will be dropped and need
no checking. Looking inside the expansion will trigger false positives.
This commit is contained in:
Samuel Tardieu 2025-01-11 12:30:17 +01:00
parent ab55d3fc62
commit 0b402baf15
3 changed files with 38 additions and 3 deletions

View file

@ -441,8 +441,9 @@ impl<'tcx> Visitor<'tcx> for SigDropHelper<'_, 'tcx> {
let parent_expr_before = self.parent_expr.replace(ex);
match ex.kind {
// Skip blocks because values in blocks will be dropped as usual.
ExprKind::Block(..) => (),
// Skip blocks because values in blocks will be dropped as usual, and await
// desugaring because temporary insides the future will have been dropped.
ExprKind::Block(..) | ExprKind::Match(_, _, MatchSource::AwaitDesugar) => (),
_ => walk_expr(self, ex),
}

View file

@ -832,4 +832,22 @@ fn should_trigger_lint_in_while_let() {
}
}
async fn foo_async(mutex: &Mutex<i32>) -> Option<MutexGuard<'_, i32>> {
Some(mutex.lock().unwrap())
}
async fn should_trigger_lint_for_async(mutex: Mutex<i32>) -> i32 {
match *foo_async(&mutex).await.unwrap() {
n if n < 10 => n,
_ => 10,
}
}
async fn should_not_trigger_lint_in_async_expansion(mutex: Mutex<i32>) -> i32 {
match foo_async(&mutex).await {
Some(guard) => *guard,
_ => 0,
}
}
fn main() {}

View file

@ -568,5 +568,21 @@ LL | }
|
= note: this might lead to deadlocks or other unexpected behavior
error: aborting due to 29 previous errors
error: temporary with significant `Drop` in `match` scrutinee will live until the end of the `match` expression
--> tests/ui/significant_drop_in_scrutinee.rs:840:11
|
LL | match *foo_async(&mutex).await.unwrap() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | }
| - temporary lives until here
|
= note: this might lead to deadlocks or other unexpected behavior
help: try moving the temporary above the match
|
LL ~ let value = *foo_async(&mutex).await.unwrap();
LL ~ match value {
|
error: aborting due to 30 previous errors