mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 01:05:39 +03:00
fix: needless_bool_assign wrongly unmangled macros
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user