Found non-binding `let` on a synchronization lockRS-E1014
A "non-binding" let
is one of the form let _ = <expr>
. Such statements
immediately drop the right-hand side expression. When non-binding
let
statements are used with synchronization locks, the guard to the lock
is immediately dropped instead of living till the end of the scope as expected,
which is often not intended. To extend the lifetime of the guard to the end of
the scope, use an underscore-prefixed name instead (i.e. _lock
). If you
want to explicitly drop the lock, std::mem::drop(lock)
conveys the intention better and is less
error-prone.
Consider revisiting this statement if the intention was not to immediately drop
the guard. Otherwise, prefer using std::mem::drop
. This issue is tuned to work
on synchronization locks from the standard library such as std::sync::Mutex
,
and those from the parking_lot
crate, such as parking_lot::RawMutex
.
Bad practice
let guard = std::sync::Mutex::new(data);
let _ = guard.lock();
let rwlock = parking_lot::RwLock::new(data);
let _ = rwlock.read();
Recommended
let guard = std::sync::Mutex::new();
let _lock = guard.lock();
let rwlock = parking_lot::RwLock::new(data);
let _read_lock = rwlock.read();