fix: needless_bool_assign wrongly unmangled macros

This commit is contained in:
Linshu Yang
2025-12-23 23:53:17 +00:00
parent d30647b7f2
commit 01b39655df
4 changed files with 56 additions and 4 deletions
+3 -3
View File
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::snippet_with_context;
use clippy_utils::sugg::Sugg;
use clippy_utils::{
SpanlessEq, get_parent_expr, higher, is_block_like, is_else_clause, is_parent_stmt, is_receiver_of_method_call,
@@ -171,8 +171,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
&& SpanlessEq::new(cx).eq_expr(lhs_a, lhs_b)
{
let mut applicability = Applicability::MachineApplicable;
let cond = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);
let lhs = snippet_with_applicability(cx, lhs_a.span, "..", &mut applicability);
let cond = Sugg::hir_with_context(cx, cond, e.span.ctxt(), "..", &mut applicability);
let (lhs, _) = snippet_with_context(cx, lhs_a.span, e.span.ctxt(), "..", &mut applicability);
let mut sugg = if a == b {
format!("{cond}; {lhs} = {a:?};")
} else {
+19
View File
@@ -42,3 +42,22 @@ fn issue15063(x: bool, y: bool) {
} else { z = x || y; }
//~^^^^^ needless_bool_assign
}
fn wrongly_unmangled_macros(must_keep: fn(usize, usize) -> bool, x: usize, y: usize) {
struct Wrapper<T>(T);
let mut skip = Wrapper(false);
macro_rules! invoke {
($func:expr, $a:expr, $b:expr) => {
$func($a, $b)
};
}
macro_rules! dot_0 {
($w:expr) => {
$w.0
};
}
dot_0!(skip) = !invoke!(must_keep, x, y);
//~^^^^^ needless_bool_assign
}
+23
View File
@@ -58,3 +58,26 @@ fn issue15063(x: bool, y: bool) {
}
//~^^^^^ needless_bool_assign
}
fn wrongly_unmangled_macros(must_keep: fn(usize, usize) -> bool, x: usize, y: usize) {
struct Wrapper<T>(T);
let mut skip = Wrapper(false);
macro_rules! invoke {
($func:expr, $a:expr, $b:expr) => {
$func($a, $b)
};
}
macro_rules! dot_0 {
($w:expr) => {
$w.0
};
}
if invoke!(must_keep, x, y) {
dot_0!(skip) = false;
} else {
dot_0!(skip) = true;
}
//~^^^^^ needless_bool_assign
}
+11 -1
View File
@@ -62,5 +62,15 @@ LL | | z = false;
LL | | }
| |_____^ help: you can reduce it to: `{ z = x || y; }`
error: aborting due to 5 previous errors
error: this if-then-else expression assigns a bool literal
--> tests/ui/needless_bool_assign.rs:77:5
|
LL | / if invoke!(must_keep, x, y) {
LL | | dot_0!(skip) = false;
LL | | } else {
LL | | dot_0!(skip) = true;
LL | | }
| |_____^ help: you can reduce it to: `dot_0!(skip) = !invoke!(must_keep, x, y);`
error: aborting due to 6 previous errors