Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::significant_drop_tightening: Searches for elements marked with #[clippy::has_significant_drop] that could be early dropped but are in fact dropped at the end of their scopes
What it meansSearches for elements marked with #[clippy::has_significant_drop] that could be early dropped but are in fact dropped at the end of their scopes. In other words, enforces the “tightening” of their possible lifetimes. It is in the nursery group and off by default.
Does it matter?
Elements marked with #[clippy::has_significant_drop] are generally synchronizing primitives that manage shared resources, as such, it is desired to release them as soon as possible to avoid unnecessary resource contention.
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
fn main() {
let lock = some_sync_resource.lock();
let owned_rslt = lock.do_stuff_with_resource();
// Only `owned_rslt` is needed but `lock` is still held.
do_heavy_computation_that_takes_time(owned_rslt);
}
Write this instead
fn main() {
let owned_rslt = some_sync_resource.lock().do_stuff_with_resource();
do_heavy_computation_that_takes_time(owned_rslt);
}
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_tightening = "warn"In code
#![warn(clippy::significant_drop_tightening)]One run
cargo clippy -- -W clippy::significant-drop-tighteningThe [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
Searches for elements marked with #[clippy::has_significant_drop] that could be early
dropped but are in fact dropped at the end of their scopes. In other words, enforces the
“tightening” of their possible lifetimes.
Why is this bad?
Elements marked with #[clippy::has_significant_drop] are generally synchronizing
primitives that manage shared resources, as such, it is desired to release them as soon as
possible to avoid unnecessary resource contention.
Example
fn main() {
let lock = some_sync_resource.lock();
let owned_rslt = lock.do_stuff_with_resource();
// Only `owned_rslt` is needed but `lock` is still held.
do_heavy_computation_that_takes_time(owned_rslt);
}
Use instead:
fn main() {
let owned_rslt = some_sync_resource.lock().do_stuff_with_resource();
do_heavy_computation_that_takes_time(owned_rslt);
}
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
- search_is_somecalled `is_some()` after searching an `Iterator` with `find`
- set_contains_or_insertusage of `HashSet::insert` after `HashSet::contains`
- significant_drop_in_scrutineeChecks for temporaries returned from function calls in a match scrutinee that have the clippy::has_significant_drop attribute
- 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
- suboptimal_flopsexponent for bases 2 and e can be computed more accurately
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?