Skip to content

Commit 8c1ea9f

Browse files
authored
Do not look for significant drop inside .await expansion (rust-lang#13985)
Temporaries created inside the expansion of `.await` will be dropped and need no checking. Looking inside the expansion will trigger false positives. changelog: [`significant_drop_in_scrutinee`]: do not falsely warn for temporaries created by `.await` expansion Fix rust-lang#13927
2 parents 6ab6c3c + 0b402ba commit 8c1ea9f

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,9 @@ impl<'tcx> Visitor<'tcx> for SigDropHelper<'_, 'tcx> {
441441
let parent_expr_before = self.parent_expr.replace(ex);
442442

443443
match ex.kind {
444-
// Skip blocks because values in blocks will be dropped as usual.
445-
ExprKind::Block(..) => (),
444+
// Skip blocks because values in blocks will be dropped as usual, and await
445+
// desugaring because temporary insides the future will have been dropped.
446+
ExprKind::Block(..) | ExprKind::Match(_, _, MatchSource::AwaitDesugar) => (),
446447
_ => walk_expr(self, ex),
447448
}
448449

tests/ui/significant_drop_in_scrutinee.rs

+18
Original file line numberDiff line numberDiff line change
@@ -832,4 +832,22 @@ fn should_trigger_lint_in_while_let() {
832832
}
833833
}
834834

835+
async fn foo_async(mutex: &Mutex<i32>) -> Option<MutexGuard<'_, i32>> {
836+
Some(mutex.lock().unwrap())
837+
}
838+
839+
async fn should_trigger_lint_for_async(mutex: Mutex<i32>) -> i32 {
840+
match *foo_async(&mutex).await.unwrap() {
841+
n if n < 10 => n,
842+
_ => 10,
843+
}
844+
}
845+
846+
async fn should_not_trigger_lint_in_async_expansion(mutex: Mutex<i32>) -> i32 {
847+
match foo_async(&mutex).await {
848+
Some(guard) => *guard,
849+
_ => 0,
850+
}
851+
}
852+
835853
fn main() {}

tests/ui/significant_drop_in_scrutinee.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -568,5 +568,21 @@ LL | }
568568
|
569569
= note: this might lead to deadlocks or other unexpected behavior
570570

571-
error: aborting due to 29 previous errors
571+
error: temporary with significant `Drop` in `match` scrutinee will live until the end of the `match` expression
572+
--> tests/ui/significant_drop_in_scrutinee.rs:840:11
573+
|
574+
LL | match *foo_async(&mutex).await.unwrap() {
575+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
576+
...
577+
LL | }
578+
| - temporary lives until here
579+
|
580+
= note: this might lead to deadlocks or other unexpected behavior
581+
help: try moving the temporary above the match
582+
|
583+
LL ~ let value = *foo_async(&mutex).await.unwrap();
584+
LL ~ match value {
585+
|
586+
587+
error: aborting due to 30 previous errors
572588

0 commit comments

Comments
 (0)