Lock guard bound to _ is dropped immediately
ID |
rust.correctness.let_underscore_lock |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Concurrency |
Language |
Rust |
Description
Binding a lock guard to _ drops it at the end of the statement, so the critical section that follows is not actually protected. Bind it to a named variable - let _guard = … - so the guard lives to the end of the scope.
Rationale
Binding a lock guard to _ drops it at the end of the statement, so the critical section that follows is not actually protected. Bind it to a named variable - let _guard = … - so the guard lives to the end of the scope.
The following code illustrates the pattern detected by this rule:
use std::sync::Mutex;
pub fn drops_guard(m: &Mutex<i32>) {
// FLAGGED: Lock guard bound to _ is dropped immediately
let _ = m.lock().unwrap();
println!("not actually holding the lock");
}