Skip to content

Commit 3ae047e

Browse files
committed
restructure messages
- The main message should point out what's wrong, not directly suggest a solution. - The second part of the message is a separate advice, so it should be emitted separately.
1 parent 85490d1 commit 3ae047e

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

clippy_lints/src/mutex_atomic.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::res::MaybeDef;
3+
use rustc_errors::Diag;
34
use rustc_hir::Expr;
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
@@ -96,14 +97,17 @@ impl<'tcx> LateLintPass<'tcx> for Mutex {
9697
&& let mutex_param = subst.type_at(0)
9798
&& let Some(atomic_name) = get_atomic_name(mutex_param)
9899
{
99-
let msg = format!(
100-
"consider using an `{atomic_name}` instead of a `Mutex` here; if you just want the locking \
101-
behavior and not the internal type, consider using `Mutex<()>`"
102-
);
100+
let msg = "using a `Mutex` where an atomic would do";
101+
let diag = |diag: &mut Diag<'_, _>| {
102+
diag.help(format!("consider using an `{atomic_name}` instead"));
103+
diag.help(
104+
"if you just want the locking behavior and not the internal type, consider using `Mutex<()>`",
105+
);
106+
};
103107
match *mutex_param.kind() {
104-
ty::Uint(t) if t != UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, msg),
105-
ty::Int(t) if t != IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, msg),
106-
_ => span_lint(cx, MUTEX_ATOMIC, expr.span, msg),
108+
ty::Uint(t) if t != UintTy::Usize => span_lint_and_then(cx, MUTEX_INTEGER, expr.span, msg, diag),
109+
ty::Int(t) if t != IntTy::Isize => span_lint_and_then(cx, MUTEX_INTEGER, expr.span, msg, diag),
110+
_ => span_lint_and_then(cx, MUTEX_ATOMIC, expr.span, msg, diag),
107111
}
108112
}
109113
}

tests/ui/mutex_atomic.stderr

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,105 @@
1-
error: consider using an `AtomicBool` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
1+
error: using a `Mutex` where an atomic would do
22
--> tests/ui/mutex_atomic.rs:7:5
33
|
44
LL | Mutex::new(true);
55
| ^^^^^^^^^^^^^^^^
66
|
7+
= help: consider using an `AtomicBool` instead
8+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
79
= note: `-D clippy::mutex-atomic` implied by `-D warnings`
810
= help: to override `-D warnings` add `#[allow(clippy::mutex_atomic)]`
911

10-
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<()>`
12+
error: using a `Mutex` where an atomic would do
1113
--> tests/ui/mutex_atomic.rs:10:5
1214
|
1315
LL | Mutex::new(5usize);
1416
| ^^^^^^^^^^^^^^^^^^
17+
|
18+
= help: consider using an `AtomicUsize` instead
19+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
1520

16-
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<()>`
21+
error: using a `Mutex` where an atomic would do
1722
--> tests/ui/mutex_atomic.rs:13:5
1823
|
1924
LL | Mutex::new(9isize);
2025
| ^^^^^^^^^^^^^^^^^^
26+
|
27+
= help: consider using an `AtomicIsize` instead
28+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
2129

22-
error: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
30+
error: using a `Mutex` where an atomic would do
2331
--> tests/ui/mutex_atomic.rs:17:5
2432
|
2533
LL | Mutex::new(&x as *const u32);
2634
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= help: consider using an `AtomicPtr` instead
37+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
2738

28-
error: consider using an `AtomicPtr` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
39+
error: using a `Mutex` where an atomic would do
2940
--> tests/ui/mutex_atomic.rs:20:5
3041
|
3142
LL | Mutex::new(&mut x as *mut u32);
3243
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
= help: consider using an `AtomicPtr` instead
46+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
3347

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<()>`
48+
error: using a `Mutex` where an atomic would do
3549
--> tests/ui/mutex_atomic.rs:23:5
3650
|
3751
LL | Mutex::new(0u32);
3852
| ^^^^^^^^^^^^^^^^
3953
|
54+
= help: consider using an `AtomicU32` instead
55+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
4056
= note: `-D clippy::mutex-integer` implied by `-D warnings`
4157
= help: to override `-D warnings` add `#[allow(clippy::mutex_integer)]`
4258

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<()>`
59+
error: using a `Mutex` where an atomic would do
4460
--> tests/ui/mutex_atomic.rs:26:5
4561
|
4662
LL | Mutex::new(0i32);
4763
| ^^^^^^^^^^^^^^^^
64+
|
65+
= help: consider using an `AtomicI32` instead
66+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
4867

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<()>`
68+
error: using a `Mutex` where an atomic would do
5069
--> tests/ui/mutex_atomic.rs:30:5
5170
|
5271
LL | Mutex::new(0u8);
5372
| ^^^^^^^^^^^^^^^
73+
|
74+
= help: consider using an `AtomicU8` instead
75+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
5476

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<()>`
77+
error: using a `Mutex` where an atomic would do
5678
--> tests/ui/mutex_atomic.rs:33:5
5779
|
5880
LL | Mutex::new(0i16);
5981
| ^^^^^^^^^^^^^^^^
82+
|
83+
= help: consider using an `AtomicI16` instead
84+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
6085

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<()>`
86+
error: using a `Mutex` where an atomic would do
6287
--> tests/ui/mutex_atomic.rs:36:25
6388
|
6489
LL | let _x: Mutex<i8> = Mutex::new(0);
6590
| ^^^^^^^^^^^^^
91+
|
92+
= help: consider using an `AtomicI8` instead
93+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
6694

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<()>`
95+
error: using a `Mutex` where an atomic would do
6896
--> tests/ui/mutex_atomic.rs:40:5
6997
|
7098
LL | Mutex::new(X);
7199
| ^^^^^^^^^^^^^
100+
|
101+
= help: consider using an `AtomicI64` instead
102+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
72103

73104
error: aborting due to 11 previous errors
74105

0 commit comments

Comments
 (0)