Skip to main content

LET_UNDERSCORE_DROP_GUARD

Static LET_UNDERSCORE_DROP_GUARD 

Source
pub static LET_UNDERSCORE_DROP_GUARD: &Lint
Expand description

The let_underscore_drop_guard lint checks for statements which don’t bind a DropGuard to anything, causing its closure to run immediately instead of at end of scope, which is typically incorrect.

§Example

ⓘ
 use std::mem::DropGuard;

 // The closure runs immediately instead of at the end of the scope,
 // which is probably not intended.
 let _ = DropGuard::new((), |_| {
     println!("cleaning up");
 });

{{produces}}

§Explanation

Statements which assign an expression to an underscore causes the expression to immediately drop instead of extending the expression’s lifetime to the end of the scope. This is usually unintended, especially for types like DropGuard, which are typically used to ensure running some cleanup code at the end of a scope.

If you want to extend the expression’s lifetime to the end of the scope, assign an underscore-prefixed name (such as _foo) to the expression. If you do actually want to drop the expression immediately, then calling std::mem::drop on the expression is clearer and helps convey intent.