Skip to content

Commit

Permalink
Do not look for significant drop inside .await expansion
Browse files Browse the repository at this point in the history
Temporaries created inside the expansion of `.await` will be dropped and need
no checking. Looking inside the expansion will trigger false positives.
  • Loading branch information
samueltardieu committed Jan 11, 2025
1 parent ab55d3f commit 0b402ba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/matches/significant_drop_in_scrutinee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ui/significant_drop_in_scrutinee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
18 changes: 17 additions & 1 deletion tests/ui/significant_drop_in_scrutinee.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b402ba

Please sign in to comment.