Skip to content

Commit b19b5f2

Browse files
committed
Auto merge of rust-lang#12008 - J-ZhengLi:issue9872, r=Jarcho
make [`mutex_atomic`] more type aware fixes: rust-lang#9872 --- changelog: [`mutex_atomic`] now suggests more specific atomic types and skips mutex i128 and u128
2 parents c992247 + a578b9b commit b19b5f2

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

clippy_lints/src/mutex_atomic.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::diagnostics::span_lint;
66
use clippy_utils::ty::is_type_diagnostic_item;
77
use rustc_hir::Expr;
88
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_middle::ty::{self, Ty};
9+
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
1010
use rustc_session::declare_lint_pass;
1111
use rustc_span::sym;
1212

@@ -105,8 +105,28 @@ impl<'tcx> LateLintPass<'tcx> for Mutex {
105105
fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> {
106106
match ty.kind() {
107107
ty::Bool => Some("AtomicBool"),
108-
ty::Uint(_) => Some("AtomicUsize"),
109-
ty::Int(_) => Some("AtomicIsize"),
108+
ty::Uint(uint_ty) => {
109+
match uint_ty {
110+
UintTy::U8 => Some("AtomicU8"),
111+
UintTy::U16 => Some("AtomicU16"),
112+
UintTy::U32 => Some("AtomicU32"),
113+
UintTy::U64 => Some("AtomicU64"),
114+
UintTy::Usize => Some("AtomicUsize"),
115+
// There's no `AtomicU128`.
116+
UintTy::U128 => None,
117+
}
118+
},
119+
ty::Int(int_ty) => {
120+
match int_ty {
121+
IntTy::I8 => Some("AtomicI8"),
122+
IntTy::I16 => Some("AtomicI16"),
123+
IntTy::I32 => Some("AtomicI32"),
124+
IntTy::I64 => Some("AtomicI64"),
125+
IntTy::Isize => Some("AtomicIsize"),
126+
// There's no `AtomicI128`.
127+
IntTy::I128 => None,
128+
}
129+
},
110130
ty::RawPtr(_) => Some("AtomicPtr"),
111131
_ => None,
112132
}

tests/ui/mutex_atomic.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ fn main() {
1818
Mutex::new(&mut x as *mut u32);
1919
//~^ ERROR: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want
2020
Mutex::new(0u32);
21-
//~^ ERROR: consider using an `AtomicUsize` instead of a `Mutex` here; if you just wan
21+
//~^ ERROR: consider using an `AtomicU32` instead of a `Mutex` here; if you just wan
2222
//~| NOTE: `-D clippy::mutex-integer` implied by `-D warnings`
2323
Mutex::new(0i32);
24-
//~^ ERROR: consider using an `AtomicIsize` instead of a `Mutex` here; if you just wan
24+
//~^ ERROR: consider using an `AtomicI32` instead of a `Mutex` here; if you just wan
2525
Mutex::new(0f32); // there are no float atomics, so this should not lint
26+
Mutex::new(0u8);
27+
//~^ ERROR: consider using an `AtomicU8` instead of a `Mutex` here; if you just wan
28+
Mutex::new(0i16);
29+
//~^ ERROR: consider using an `AtomicI16` instead of a `Mutex` here; if you just wan
30+
let _x: Mutex<i8> = Mutex::new(0);
31+
//~^ ERROR: consider using an `AtomicI8` instead of a `Mutex` here; if you just wan
32+
const X: i64 = 0;
33+
Mutex::new(X);
34+
//~^ ERROR: consider using an `AtomicI64` instead of a `Mutex` here; if you just wan
35+
36+
// there are no 128 atomics, so these two should not lint
37+
{
38+
Mutex::new(0u128);
39+
let _x: Mutex<i128> = Mutex::new(0);
40+
}
2641
}

tests/ui/mutex_atomic.stderr

+27-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ error: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want
3131
LL | Mutex::new(&mut x as *mut u32);
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333

34-
error: consider using an `AtomicUsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
34+
error: consider using an `AtomicU32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
3535
--> $DIR/mutex_atomic.rs:20:5
3636
|
3737
LL | Mutex::new(0u32);
@@ -40,11 +40,35 @@ LL | Mutex::new(0u32);
4040
= note: `-D clippy::mutex-integer` implied by `-D warnings`
4141
= help: to override `-D warnings` add `#[allow(clippy::mutex_integer)]`
4242

43-
error: consider using an `AtomicIsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
43+
error: consider using an `AtomicI32` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
4444
--> $DIR/mutex_atomic.rs:23:5
4545
|
4646
LL | Mutex::new(0i32);
4747
| ^^^^^^^^^^^^^^^^
4848

49-
error: aborting due to 7 previous errors
49+
error: consider using an `AtomicU8` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
50+
--> $DIR/mutex_atomic.rs:26:5
51+
|
52+
LL | Mutex::new(0u8);
53+
| ^^^^^^^^^^^^^^^
54+
55+
error: consider using an `AtomicI16` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
56+
--> $DIR/mutex_atomic.rs:28:5
57+
|
58+
LL | Mutex::new(0i16);
59+
| ^^^^^^^^^^^^^^^^
60+
61+
error: consider using an `AtomicI8` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
62+
--> $DIR/mutex_atomic.rs:30:25
63+
|
64+
LL | let _x: Mutex<i8> = Mutex::new(0);
65+
| ^^^^^^^^^^^^^
66+
67+
error: consider using an `AtomicI64` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
68+
--> $DIR/mutex_atomic.rs:33:5
69+
|
70+
LL | Mutex::new(X);
71+
| ^^^^^^^^^^^^^
72+
73+
error: aborting due to 11 previous errors
5074

0 commit comments

Comments
 (0)