-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clippy::mut_mutex_lock
after an immutable reference is held
#9854
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
Comments
Yes |
+1 when writing |
rshearman
added a commit
to rshearman/rust-clippy
that referenced
this issue
Jul 18, 2024
When there is a pointer chain where one of the pointers isn't mutable then this results in a false-positive for `mut_mutex_lock` as it only checks the mutability of the first pointer level. Fix this by using `peel_mid_ty_refs_is_mutable` which correctly determines whether the pointer is ultimately mutable and thus whether `Mutex::get_lock()` can actually be used. Fixes rust-lang#9854
rshearman
added a commit
to rshearman/rust-clippy
that referenced
this issue
Jul 18, 2024
When there is are multiple references where one of the references isn't mutable then this results in a false-positive for `mut_mutex_lock` as it only checks the mutability of the first reference level. Fix this by using `peel_mid_ty_refs_is_mutable` which correctly determines whether the reference is ultimately mutable and thus whether `Mutex::get_lock()` can actually be used. Fixes rust-lang#9854
bors
added a commit
that referenced
this issue
Oct 1, 2024
Fix `mut_mutex_lock` when reference not ultimately mutable When there is are multiple references where one of the references isn't mutable then this results in a false-positive for `mut_mutex_lock` as it only checks the mutability of the first reference level. Fix this by using `peel_mid_ty_refs_is_mutable` which correctly determines whether the reference is ultimately mutable and thus whether `Mutex::get_lock()` can actually be used. Fixes #9854 changelog: [`mut_mutex_lock`]: No longer lints if the mutex is behind multiple references and one of those references isn't mutable
I think this bug may still be present. Reproducer (playground): use std::sync::Mutex;
struct Struct<'a> {
ref_mut_mutex: &'a mut Mutex<i32>,
}
impl PartialEq for Struct<'_> {
fn eq(&self, other: &Self) -> bool {
let x = self.ref_mut_mutex.lock().unwrap();
// The following suggested line doesn't compile:
// let x = self.ref_mut_mutex.get_mut().unwrap();
let y = other.ref_mut_mutex.lock().unwrap();
*x == *y
}
} Clippy produces the following warnings:
However, applying the suggestion produces this error:
|
cc: @rshearman @blyxyas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
Summary
When an immutable reference is held, Clippy still suggests using
get_mut()
instead oflock()
for Mutexes, even though this does not work, since it cannot borrow a mutable reference at the same time as an immutable reference.Lint Name
mut_mutex_lock
Reproducer
I tried this code:
I saw this happen:
When following Clippy's suggestion:
I expected to see this happen:
No lint suggested
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: