diff --git a/clippy_lints/src/mutex_atomic.rs b/clippy_lints/src/mutex_atomic.rs index c92bad1393ca..1cb6b901723f 100644 --- a/clippy_lints/src/mutex_atomic.rs +++ b/clippy_lints/src/mutex_atomic.rs @@ -5,6 +5,7 @@ use clippy_utils::ty::ty_from_hir_ty; use rustc_errors::{Applicability, Diag}; use rustc_hir::{Expr, ExprKind, Item, ItemKind, LetStmt, QPath}; use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::mir::Mutability; use rustc_middle::ty::{self, IntTy, Ty, UintTy}; use rustc_session::declare_lint_pass; use rustc_span::sym; @@ -169,7 +170,8 @@ fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> { IntTy::I128 => None, } }, - ty::RawPtr(_, _) => Some("AtomicPtr"), + // `AtomicPtr` only accepts `*mut T` + ty::RawPtr(_, Mutability::Mut) => Some("AtomicPtr"), _ => None, } } diff --git a/tests/ui/mutex_atomic.rs b/tests/ui/mutex_atomic.rs index 331c09ab1a71..b907c3081841 100644 --- a/tests/ui/mutex_atomic.rs +++ b/tests/ui/mutex_atomic.rs @@ -16,8 +16,8 @@ fn main() { //~^ mutex_atomic let mut x = 4u32; + // `AtomicPtr` only accepts `*mut T`, so this should not lint let _ = Mutex::new(&x as *const u32); - //~^ mutex_atomic let _ = Mutex::new(&mut x as *mut u32); //~^ mutex_atomic diff --git a/tests/ui/mutex_atomic.stderr b/tests/ui/mutex_atomic.stderr index 9afbf72dc67d..9d8a7ccb33bd 100644 --- a/tests/ui/mutex_atomic.stderr +++ b/tests/ui/mutex_atomic.stderr @@ -24,14 +24,6 @@ LL | let _ = Mutex::new(9isize); | = help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>` -error: using a `Mutex` where an atomic would do - --> tests/ui/mutex_atomic.rs:19:13 - | -LL | let _ = Mutex::new(&x as *const u32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicPtr::new(&x as *const u32)` - | - = help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>` - error: using a `Mutex` where an atomic would do --> tests/ui/mutex_atomic.rs:22:13 | @@ -115,5 +107,5 @@ LL | let reassigned = mtx; = help: consider using an `AtomicI32` instead = help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>` -error: aborting due to 14 previous errors +error: aborting due to 13 previous errors