Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

significant_drop_in_scrutinee fires when matching on async function result #13927

Open
jakubdabek opened this issue Jan 2, 2025 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@jakubdabek
Copy link

Summary

When calling an async function that returns a MutexGuard the lint fires.

Lint Name

significant_drop_in_scrutinee

Reproducer

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9193048ab64be887f2aa4cb7af02e490

use std::sync::Mutex;
use std::sync::MutexGuard;

async fn foo_async(mutex: &Mutex<i32>) -> Option<MutexGuard<'_, i32>> {
    Some(mutex.lock().unwrap())
}

#[warn(clippy::significant_drop_in_scrutinee)]
async fn bar_async() {
    let mutex = Mutex::new(1);
    let locked = match foo_async(&mutex).await {
        Some(locked) => locked,
        None => panic!("unavailable"),
    };
    
    println!("{}", *locked);
}

I saw this happen:

warning: temporary with significant `Drop` in `match` scrutinee will live until the end of the `match` expression
  --> src/main.rs:12:42
   |
12 |     let locked = match foo_async(&mutex).await {
   |                                          ^^^^^
...
15 |     };
   |      - temporary lives until here
   |
   = note: this might lead to deadlocks or other unexpected behavior
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_in_scrutinee
note: the lint level is defined here
  --> src/main.rs:9:8
   |
9  | #[warn(clippy::significant_drop_in_scrutinee)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try moving the temporary above the match
   |
12 ~     let locked = await;
13 ~     match foo_async(&mutex).() {
   |

I expected to see this happen:
No lint emitted, we're destructuring a value with the MutexGuard itself and assigning it.
At first I thought this was a problem with the code that has only that, i.e. matching on some fn foo(mutex: &Mutex<T>) -> Option<MutexGuard<'_, T>>, but that's fine.

Secondly, I think this has something to do with the await desugaring, it also breaks the suggestion:

help: try moving the temporary above the match
   |
12 ~     let locked = await;
13 ~     match foo_async(&mutex).() {
   |

It even fires without the Option, but that's kinda degenerate code:

    let locked = match foo(&mutex).await {
        locked => locked,
    };

Version

Clippy 0.1.85 (2024-12-19 9e136a30a9) via The Playground

Additional Labels

@rustbot label +I-suggestion-causes-error

@jakubdabek jakubdabek added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 2, 2025
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

2 participants