Skip to content

Commit 99ce639

Browse files
committed
suggest replacing Mutex::new with AtomicX::new
1 parent 38ac3d0 commit 99ce639

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

clippy_lints/src/mutex_atomic.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::res::MaybeDef;
3+
use clippy_utils::sugg::Sugg;
34
use clippy_utils::ty::ty_from_hir_ty;
4-
use rustc_errors::Diag;
5-
use rustc_hir::{Expr, Item, ItemKind, LetStmt};
5+
use rustc_errors::{Applicability, Diag};
6+
use rustc_hir::{Expr, ExprKind, Item, ItemKind, LetStmt, QPath};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
89
use rustc_session::declare_lint_pass;
@@ -120,7 +121,19 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, ty: Ty<'tcx>) {
120121
{
121122
let msg = "using a `Mutex` where an atomic would do";
122123
let diag = |diag: &mut Diag<'_, _>| {
123-
diag.help(format!("consider using an `{atomic_name}` instead"));
124+
// if `expr = Mutex::new(arg)`, we can try emitting a suggestion
125+
if let ExprKind::Call(qpath, [arg]) = expr.kind
126+
&& let ExprKind::Path(QPath::TypeRelative(_mutex, new)) = qpath.kind
127+
&& new.ident.name == sym::new
128+
{
129+
let mut applicability = Applicability::MaybeIncorrect;
130+
let arg = Sugg::hir_with_applicability(cx, arg, "_", &mut applicability);
131+
132+
let suggs = vec![(expr.span, format!("std::sync::atomic::{atomic_name}::new({arg})"))];
133+
diag.multipart_suggestion("try", suggs, applicability);
134+
} else {
135+
diag.help(format!("consider using an `{atomic_name}` instead"));
136+
}
124137
diag.help("if you just want the locking behavior and not the internal type, consider using `Mutex<()>`");
125138
};
126139
match *mutex_param.kind() {

tests/ui/mutex_atomic.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@no-rustfix
12
#![warn(clippy::mutex_integer)]
23
#![warn(clippy::mutex_atomic)]
34
#![allow(clippy::borrow_as_ptr)]
@@ -48,11 +49,11 @@ fn main() {
4849
}
4950
}
5051

51-
static MTX: Mutex<u32> = Mutex::new(0);
52-
//~^ mutex_integer
53-
5452
// don't lint on _use_, only declaration
5553
fn issue13378() {
54+
static MTX: Mutex<u32> = Mutex::new(0);
55+
//~^ mutex_integer
56+
5657
let mut guard = MTX.lock().unwrap();
5758
*guard += 1;
5859

tests/ui/mutex_atomic.stderr

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,113 @@
11
error: using a `Mutex` where an atomic would do
2-
--> tests/ui/mutex_atomic.rs:8:13
2+
--> tests/ui/mutex_atomic.rs:9:13
33
|
44
LL | let _ = Mutex::new(true);
5-
| ^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicBool::new(true)`
66
|
7-
= help: consider using an `AtomicBool` instead
87
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
98
= note: `-D clippy::mutex-atomic` implied by `-D warnings`
109
= help: to override `-D warnings` add `#[allow(clippy::mutex_atomic)]`
1110

1211
error: using a `Mutex` where an atomic would do
13-
--> tests/ui/mutex_atomic.rs:11:13
12+
--> tests/ui/mutex_atomic.rs:12:13
1413
|
1514
LL | let _ = Mutex::new(5usize);
16-
| ^^^^^^^^^^^^^^^^^^
15+
| ^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicUsize::new(5usize)`
1716
|
18-
= help: consider using an `AtomicUsize` instead
1917
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
2018

2119
error: using a `Mutex` where an atomic would do
22-
--> tests/ui/mutex_atomic.rs:14:13
20+
--> tests/ui/mutex_atomic.rs:15:13
2321
|
2422
LL | let _ = Mutex::new(9isize);
25-
| ^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicIsize::new(9isize)`
2624
|
27-
= help: consider using an `AtomicIsize` instead
2825
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
2926

3027
error: using a `Mutex` where an atomic would do
31-
--> tests/ui/mutex_atomic.rs:18:13
28+
--> tests/ui/mutex_atomic.rs:19:13
3229
|
3330
LL | let _ = Mutex::new(&x as *const u32);
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicPtr::new(&x as *const u32)`
3532
|
36-
= help: consider using an `AtomicPtr` instead
3733
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
3834

3935
error: using a `Mutex` where an atomic would do
40-
--> tests/ui/mutex_atomic.rs:21:13
36+
--> tests/ui/mutex_atomic.rs:22:13
4137
|
4238
LL | let _ = Mutex::new(&mut x as *mut u32);
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicPtr::new(&mut x as *mut u32)`
4440
|
45-
= help: consider using an `AtomicPtr` instead
4641
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
4742

4843
error: using a `Mutex` where an atomic would do
49-
--> tests/ui/mutex_atomic.rs:24:13
44+
--> tests/ui/mutex_atomic.rs:25:13
5045
|
5146
LL | let _ = Mutex::new(0u32);
52-
| ^^^^^^^^^^^^^^^^
47+
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicU32::new(0u32)`
5348
|
54-
= help: consider using an `AtomicU32` instead
5549
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
5650
= note: `-D clippy::mutex-integer` implied by `-D warnings`
5751
= help: to override `-D warnings` add `#[allow(clippy::mutex_integer)]`
5852

5953
error: using a `Mutex` where an atomic would do
60-
--> tests/ui/mutex_atomic.rs:27:13
54+
--> tests/ui/mutex_atomic.rs:28:13
6155
|
6256
LL | let _ = Mutex::new(0i32);
63-
| ^^^^^^^^^^^^^^^^
57+
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicI32::new(0i32)`
6458
|
65-
= help: consider using an `AtomicI32` instead
6659
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
6760

6861
error: using a `Mutex` where an atomic would do
69-
--> tests/ui/mutex_atomic.rs:31:13
62+
--> tests/ui/mutex_atomic.rs:32:13
7063
|
7164
LL | let _ = Mutex::new(0u8);
72-
| ^^^^^^^^^^^^^^^
65+
| ^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicU8::new(0u8)`
7366
|
74-
= help: consider using an `AtomicU8` instead
7567
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
7668

7769
error: using a `Mutex` where an atomic would do
78-
--> tests/ui/mutex_atomic.rs:34:13
70+
--> tests/ui/mutex_atomic.rs:35:13
7971
|
8072
LL | let _ = Mutex::new(0i16);
81-
| ^^^^^^^^^^^^^^^^
73+
| ^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicI16::new(0i16)`
8274
|
83-
= help: consider using an `AtomicI16` instead
8475
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
8576

8677
error: using a `Mutex` where an atomic would do
87-
--> tests/ui/mutex_atomic.rs:37:25
78+
--> tests/ui/mutex_atomic.rs:38:25
8879
|
8980
LL | let _x: Mutex<i8> = Mutex::new(0);
90-
| ^^^^^^^^^^^^^
81+
| ^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicI8::new(0)`
9182
|
92-
= help: consider using an `AtomicI8` instead
9383
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
9484

9585
error: using a `Mutex` where an atomic would do
96-
--> tests/ui/mutex_atomic.rs:41:13
86+
--> tests/ui/mutex_atomic.rs:42:13
9787
|
9888
LL | let _ = Mutex::new(X);
99-
| ^^^^^^^^^^^^^
89+
| ^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicI64::new(X)`
10090
|
101-
= help: consider using an `AtomicI64` instead
10291
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
10392

10493
error: using a `Mutex` where an atomic would do
105-
--> tests/ui/mutex_atomic.rs:51:26
94+
--> tests/ui/mutex_atomic.rs:54:30
10695
|
107-
LL | static MTX: Mutex<u32> = Mutex::new(0);
108-
| ^^^^^^^^^^^^^
96+
LL | static MTX: Mutex<u32> = Mutex::new(0);
97+
| ^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicU32::new(0)`
10998
|
110-
= help: consider using an `AtomicU32` instead
11199
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
112100

113101
error: using a `Mutex` where an atomic would do
114-
--> tests/ui/mutex_atomic.rs:59:15
102+
--> tests/ui/mutex_atomic.rs:60:15
115103
|
116104
LL | let mtx = Mutex::new(0);
117-
| ^^^^^^^^^^^^^
105+
| ^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicI32::new(0)`
118106
|
119-
= help: consider using an `AtomicI32` instead
120107
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
121108

122109
error: using a `Mutex` where an atomic would do
123-
--> tests/ui/mutex_atomic.rs:63:22
110+
--> tests/ui/mutex_atomic.rs:64:22
124111
|
125112
LL | let reassigned = mtx;
126113
| ^^^

0 commit comments

Comments
 (0)