Email me

Clippy lints / Nursery

Clippy lint · Nursery · checked with Clippy 0.1.99

clippy::significant_drop_in_scrutinee: Checks for temporaries returned from function calls in a match scrutinee that have the clippy::has_significant_drop attribute

What it meansChecks for temporaries returned from function calls in a match scrutinee that have the clippy::has_significant_drop attribute. It is in the nursery group and off by default.

Does it matter?

The clippy::has_significant_drop attribute can be added to types whose Drop impls have an important side-effect, such as unlocking a mutex, making it important for users to be able to accurately understand their lifetimes. When a temporary is returned in a function call in a match scrutinee, its lifetime lasts until the end of the match block, which may be surprising. For Mutexes this can lead to a deadlock. This happens when the match scrutinee uses a function call that returns a MutexGuard and then tries to lock again in one of the match arms. In that case the MutexGuard in the scrutinee will not be dropped until the end of the match block and thus will not unlock.

New lints still being finished. Off by default and may have false positives.

How to fix it

Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:

Flagged

let mutex = Mutex::new(State {});

match mutex.lock().unwrap().foo() {
    true => {
        mutex.lock().unwrap().bar(); // Deadlock!
    }
    false => {}
};

println!("All done!");

Write this instead

let mutex = Mutex::new(State {});

let is_foo = mutex.lock().unwrap().foo();
match is_foo {
    true => {
        mutex.lock().unwrap().bar();
    }
    false => {}
};

println!("All done!");

Turn it off, or on

It is off by default. To use it, set it to warn or deny:

Whole crate (Cargo.toml)

[lints.clippy]
significant_drop_in_scrutinee = "warn"

In code

#![warn(clippy::significant_drop_in_scrutinee)]

One run

cargo clippy -- -W clippy::significant-drop-in-scrutinee

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Nurserygroup
allowdefault level
nofixed by cargo clippy --fix
1.60.0added in Rust

Clippy's own documentation

What it does

Checks for temporaries returned from function calls in a match scrutinee that have the clippy::has_significant_drop attribute.

Why is this bad?

The clippy::has_significant_drop attribute can be added to types whose Drop impls have an important side-effect, such as unlocking a mutex, making it important for users to be able to accurately understand their lifetimes. When a temporary is returned in a function call in a match scrutinee, its lifetime lasts until the end of the match block, which may be surprising.

For Mutexes this can lead to a deadlock. This happens when the match scrutinee uses a function call that returns a MutexGuard and then tries to lock again in one of the match arms. In that case the MutexGuard in the scrutinee will not be dropped until the end of the match block and thus will not unlock.

Example

let mutex = Mutex::new(State {});

match mutex.lock().unwrap().foo() {
    true => {
        mutex.lock().unwrap().bar(); // Deadlock!
    }
    false => {}
};

println!("All done!");

Use instead:

let mutex = Mutex::new(State {});

let is_foo = mutex.lock().unwrap().foo();
match is_foo {
    true => {
        mutex.lock().unwrap().bar();
    }
    false => {}
};

println!("All done!");

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ