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.
This commit is contained in:
parent
85490d1845
commit
3ae047ee04
2 changed files with 54 additions and 19 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use clippy_utils::diagnostics::span_lint;
|
||||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::res::MaybeDef;
|
||||
use rustc_errors::Diag;
|
||||
use rustc_hir::Expr;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
|
||||
|
|
@ -96,14 +97,17 @@ impl<'tcx> LateLintPass<'tcx> for Mutex {
|
|||
&& let mutex_param = subst.type_at(0)
|
||||
&& let Some(atomic_name) = get_atomic_name(mutex_param)
|
||||
{
|
||||
let msg = format!(
|
||||
"consider using an `{atomic_name}` instead of a `Mutex` here; if you just want the locking \
|
||||
behavior and not the internal type, consider using `Mutex<()>`"
|
||||
);
|
||||
let msg = "using a `Mutex` where an atomic would do";
|
||||
let diag = |diag: &mut Diag<'_, _>| {
|
||||
diag.help(format!("consider using an `{atomic_name}` instead"));
|
||||
diag.help(
|
||||
"if you just want the locking behavior and not the internal type, consider using `Mutex<()>`",
|
||||
);
|
||||
};
|
||||
match *mutex_param.kind() {
|
||||
ty::Uint(t) if t != UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, msg),
|
||||
ty::Int(t) if t != IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, msg),
|
||||
_ => span_lint(cx, MUTEX_ATOMIC, expr.span, msg),
|
||||
ty::Uint(t) if t != UintTy::Usize => span_lint_and_then(cx, MUTEX_INTEGER, expr.span, msg, diag),
|
||||
ty::Int(t) if t != IntTy::Isize => span_lint_and_then(cx, MUTEX_INTEGER, expr.span, msg, diag),
|
||||
_ => span_lint_and_then(cx, MUTEX_ATOMIC, expr.span, msg, diag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,74 +1,105 @@
|
|||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:7:5
|
||||
|
|
||||
LL | Mutex::new(true);
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicBool` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
= note: `-D clippy::mutex-atomic` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::mutex_atomic)]`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:10:5
|
||||
|
|
||||
LL | Mutex::new(5usize);
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicUsize` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:13:5
|
||||
|
|
||||
LL | Mutex::new(9isize);
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicIsize` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:17:5
|
||||
|
|
||||
LL | Mutex::new(&x as *const u32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicPtr` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:20:5
|
||||
|
|
||||
LL | Mutex::new(&mut x as *mut u32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicPtr` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:23:5
|
||||
|
|
||||
LL | Mutex::new(0u32);
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicU32` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
= note: `-D clippy::mutex-integer` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::mutex_integer)]`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:26:5
|
||||
|
|
||||
LL | Mutex::new(0i32);
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicI32` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:30:5
|
||||
|
|
||||
LL | Mutex::new(0u8);
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicU8` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:33:5
|
||||
|
|
||||
LL | Mutex::new(0i16);
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicI16` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:36:25
|
||||
|
|
||||
LL | let _x: Mutex<i8> = Mutex::new(0);
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicI8` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
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<()>`
|
||||
error: using a `Mutex` where an atomic would do
|
||||
--> tests/ui/mutex_atomic.rs:40:5
|
||||
|
|
||||
LL | Mutex::new(X);
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider using an `AtomicI64` instead
|
||||
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
|
||||
|
||||
error: aborting due to 11 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue