fix: match_bool wrongly unmangled macros

This commit is contained in:
Linshu Yang
2026-01-02 23:13:18 +00:00
parent 0cfbe56d04
commit 9d08eb487b
4 changed files with 40 additions and 3 deletions
+3 -2
View File
@@ -25,8 +25,9 @@ pub(crate) fn check(cx: &LateContext<'_>, scrutinee: &Expr<'_>, arms: &[Arm<'_>]
"`match` on a boolean expression",
move |diag| {
let mut app = Applicability::MachineApplicable;
let ctxt = expr.span.ctxt();
let test_sugg = if let PatKind::Expr(arm_bool) = arms[0].pat.kind {
let test = Sugg::hir_with_applicability(cx, scrutinee, "_", &mut app);
let test = Sugg::hir_with_context(cx, scrutinee, ctxt, "_", &mut app);
if let PatExprKind::Lit { lit, .. } = arm_bool.kind {
match &lit.node {
LitKind::Bool(true) => Some(test),
@@ -36,7 +37,7 @@ pub(crate) fn check(cx: &LateContext<'_>, scrutinee: &Expr<'_>, arms: &[Arm<'_>]
.map(|test| {
if let Some(guard) = &arms[0]
.guard
.map(|g| Sugg::hir_with_applicability(cx, g, "_", &mut app))
.map(|g| Sugg::hir_with_context(cx, g, ctxt, "_", &mut app))
{
test.and(guard)
} else {
+11
View File
@@ -74,4 +74,15 @@ fn issue15351() {
}
}
fn wrongly_unmangled_macros() {
macro_rules! test_expr {
($val:expr) => {
($val + 1) > 0
};
}
let x = 5;
if test_expr!(x) { 1 } else { 0 };
}
fn main() {}
+15
View File
@@ -126,4 +126,19 @@ fn issue15351() {
}
}
fn wrongly_unmangled_macros() {
macro_rules! test_expr {
($val:expr) => {
($val + 1) > 0
};
}
let x = 5;
match test_expr!(x) {
//~^ match_bool
true => 1,
false => 0,
};
}
fn main() {}
+11 -1
View File
@@ -183,5 +183,15 @@ LL + break 'a;
LL + } }
|
error: aborting due to 13 previous errors
error: `match` on a boolean expression
--> tests/ui/match_bool.rs:137:5
|
LL | / match test_expr!(x) {
LL | |
LL | | true => 1,
LL | | false => 0,
LL | | };
| |_____^ help: consider using an `if`/`else` expression: `if test_expr!(x) { 1 } else { 0 }`
error: aborting due to 14 previous errors