-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Closed
Copy link
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
clippy::mutex_integer lints on usages as well as definitions, which seems redundant (and could be unactionable if this happens cross-crate).
Reproducer
I tried this code (playground):
#![allow(dead_code)]
#![warn(clippy::mutex_integer)]
use std::sync::Mutex;
// `Mutex` over `AtomicU32` to avoid a race between decrementing the guard count
// to 0 and enabling interrupts (not that it matters for this example...)
static NUM_INTERRUPTS_DISABLED_GUARDS: Mutex<u32> = Mutex::new(0);
#[non_exhaustive]
struct InterruptsDisabledGuard;
impl Drop for InterruptsDisabledGuard {
fn drop(&mut self) {
// Holds the lock for the entire function so that nobody tries to
// increment the guard count right before interrupts are enabled
let mut guard_count = NUM_INTERRUPTS_DISABLED_GUARDS.lock().unwrap();
*guard_count -= 1;
if *guard_count == 0 {
println!("Interrupts enabled! Anything could happen!");
}
}
}
fn disable_interrupts() -> InterruptsDisabledGuard {
*NUM_INTERRUPTS_DISABLED_GUARDS.lock().unwrap() += 1;
println!("Interrupts disabled...");
InterruptsDisabledGuard
}I expected to see this happen:
warning: consider using an `AtomicU32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> src/lib.rs:8:53
|
8 | static NUM_INTERRUPTS_DISABLED_GUARDS: Mutex<u32> = Mutex::new(0);
| ^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
note: the lint level is defined here
--> src/lib.rs:2:9
|
2 | #![warn(clippy::mutex_integer)]
| ^^^^^^^^^^^^^^^^^^^^^
Instead, this happened:
warning: consider using an `AtomicU32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> src/lib.rs:8:53
|
8 | static NUM_INTERRUPTS_DISABLED_GUARDS: Mutex<u32> = Mutex::new(0);
| ^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
note: the lint level is defined here
--> src/lib.rs:2:9
|
2 | #![warn(clippy::mutex_integer)]
| ^^^^^^^^^^^^^^^^^^^^^
warning: consider using an `AtomicU32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> src/lib.rs:17:31
|
17 | let mut guard_count = NUM_INTERRUPTS_DISABLED_GUARDS.lock().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
warning: consider using an `AtomicU32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
--> src/lib.rs:27:6
|
27 | *NUM_INTERRUPTS_DISABLED_GUARDS.lock().unwrap() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
Version
1.83.0-nightly (2024-09-05 9c01301c52df5d2d7b6f) on the Rust Playground
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have