Skip to content

Commit 9268b9a

Browse files
committed
fix: mutex_atomic wrongly unmangled macros
1 parent 52fb591 commit 9268b9a

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

clippy_lints/src/mutex_atomic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn check_expr<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, ty_ascription: &T
143143
&& new.ident.name == sym::new
144144
{
145145
let mut applicability = Applicability::MaybeIncorrect;
146-
let arg = Sugg::hir_with_applicability(cx, arg, "_", &mut applicability);
146+
let arg = Sugg::hir_with_context(cx, arg, expr.span.ctxt(), "_", &mut applicability);
147147
let mut suggs = vec![(expr.span, format!("std::sync::atomic::{atomic_name}::new({arg})"))];
148148
match ty_ascription {
149149
TypeAscriptionKind::Required(ty_ascription) => {

tests/ui/mutex_atomic.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ fn issue13378() {
6565
let (funky_mtx) = std::sync::atomic::AtomicU64::new(0);
6666
//~^ mutex_integer
6767
}
68+
69+
fn wrongly_unmangled_macros() {
70+
macro_rules! test_expr {
71+
($val:expr) => {
72+
($val > 0 && true)
73+
};
74+
}
75+
76+
let _ = std::sync::atomic::AtomicBool::new(test_expr!(1));
77+
//~^ mutex_atomic
78+
// The suggestion should preserve the macro call: `AtomicBool::new(test_expr!(true))`
79+
}

tests/ui/mutex_atomic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ fn issue13378() {
6565
let (funky_mtx): Mutex<u64> = Mutex::new(0);
6666
//~^ mutex_integer
6767
}
68+
69+
fn wrongly_unmangled_macros() {
70+
macro_rules! test_expr {
71+
($val:expr) => {
72+
($val > 0 && true)
73+
};
74+
}
75+
76+
let _ = Mutex::new(test_expr!(1));
77+
//~^ mutex_atomic
78+
// The suggestion should preserve the macro call: `AtomicBool::new(test_expr!(true))`
79+
}

tests/ui/mutex_atomic.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,13 @@ LL - let (funky_mtx): Mutex<u64> = Mutex::new(0);
130130
LL + let (funky_mtx) = std::sync::atomic::AtomicU64::new(0);
131131
|
132132

133-
error: aborting due to 14 previous errors
133+
error: using a `Mutex` where an atomic would do
134+
--> tests/ui/mutex_atomic.rs:76:13
135+
|
136+
LL | let _ = Mutex::new(test_expr!(1));
137+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::atomic::AtomicBool::new(test_expr!(1))`
138+
|
139+
= help: if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
140+
141+
error: aborting due to 15 previous errors
134142

0 commit comments

Comments
 (0)