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-scrutineeThe [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.
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
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- redundant_pub_cratepub(crate) function inside private module
- search_is_somecalled `is_some()` after searching an `Iterator` with `find`
- set_contains_or_insertusage of `HashSet::insert` after `HashSet::contains`
- significant_drop_tighteningSearches for elements marked with #[clippy::has_significant_drop] that could be early dropped but are in fact dropped at the end of their scopes
- single_option_map`fn` that only maps over argument
- string_lit_as_bytesChecks for the as_bytes method called on string literals that contain only ASCII characters
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?