Skip to content

Commit e5fd571

Browse files
committed
realize that a test case is incorrect
`Mutex<*const _>` doesn't make a lot of sense (there can be no contention over a read-only reference), but `AtomicPtr::new(*const _)` straight up doesn't compile
1 parent 99ce639 commit e5fd571

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

clippy_lints/src/mutex_atomic.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use clippy_utils::ty::ty_from_hir_ty;
55
use rustc_errors::{Applicability, Diag};
66
use rustc_hir::{Expr, ExprKind, Item, ItemKind, LetStmt, QPath};
77
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::mir::Mutability;
89
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
910
use rustc_session::declare_lint_pass;
1011
use rustc_span::sym;
@@ -169,7 +170,8 @@ fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> {
169170
IntTy::I128 => None,
170171
}
171172
},
172-
ty::RawPtr(_, _) => Some("AtomicPtr"),
173+
// `AtomicPtr` only accepts `*mut T`
174+
ty::RawPtr(_, Mutability::Mut) => Some("AtomicPtr"),
173175
_ => None,
174176
}
175177
}

tests/ui/mutex_atomic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fn main() {
1616
//~^ mutex_atomic
1717

1818
let mut x = 4u32;
19+
// `AtomicPtr` only accepts `*mut T`, so this should not lint
1920
let _ = Mutex::new(&x as *const u32);
20-
//~^ mutex_atomic
2121

2222
let _ = Mutex::new(&mut x as *mut u32);
2323
//~^ mutex_atomic

tests/ui/mutex_atomic.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ LL | let _ = Mutex::new(9isize);
2424
|
2525
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
2626

27-
error: using a `Mutex` where an atomic would do
28-
--> tests/ui/mutex_atomic.rs:19:13
29-
|
30-
LL | let _ = Mutex::new(&x as *const u32);
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicPtr::new(&x as *const u32)`
32-
|
33-
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
34-
3527
error: using a `Mutex` where an atomic would do
3628
--> tests/ui/mutex_atomic.rs:22:13
3729
|
@@ -115,5 +107,5 @@ LL | let reassigned = mtx;
115107
= help: consider using an `AtomicI32` instead
116108
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
117109

118-
error: aborting due to 14 previous errors
110+
error: aborting due to 13 previous errors
119111

0 commit comments

Comments
 (0)