fix(needless_if): don't expand macro invocations in the suggestion

This commit is contained in:
Ada Alakbarova 2025-10-26 15:34:23 +01:00
parent d1b51eafaa
commit ce2ef31ed6
No known key found for this signature in database
4 changed files with 34 additions and 3 deletions

View file

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::higher::If;
use clippy_utils::is_from_proc_macro;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::{SpanRangeExt, walk_span_to_context};
use rustc_errors::Applicability;
use rustc_hir::{ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
@ -56,7 +56,8 @@ impl LateLintPass<'_> for NeedlessIf {
src.bytes()
.all(|ch| matches!(ch, b'{' | b'}') || ch.is_ascii_whitespace())
})
&& let Some(cond_snippet) = cond.span.get_source_text(cx)
&& let Some(cond_span) = walk_span_to_context(cond.span, expr.span.ctxt())
&& let Some(cond_snippet) = cond_span.get_source_text(cx)
&& !is_from_proc_macro(cx, expr)
{
span_lint_and_sugg(

View file

@ -104,3 +104,12 @@ fn main() {
let () = if maybe_side_effect() {};
}
fn issue15960() -> i32 {
matches!(2, 3);
//~^ needless_if
matches!(2, 3) == (2 * 2 == 5);
//~^ needless_if
1 // put something here so that `if` is a statement not an expression
}

View file

@ -105,3 +105,12 @@ fn main() {
let () = if maybe_side_effect() {};
}
fn issue15960() -> i32 {
if matches!(2, 3) {}
//~^ needless_if
if matches!(2, 3) == (2 * 2 == 5) {}
//~^ needless_if
1 // put something here so that `if` is a statement not an expression
}

View file

@ -74,5 +74,17 @@ error: this `if` branch is empty
LL | if true {}
| ^^^^^^^^^^ help: you can remove it: `true;`
error: aborting due to 7 previous errors
error: this `if` branch is empty
--> tests/ui/needless_if.rs:110:5
|
LL | if matches!(2, 3) {}
| ^^^^^^^^^^^^^^^^^^^^ help: you can remove it: `matches!(2, 3);`
error: this `if` branch is empty
--> tests/ui/needless_if.rs:112:5
|
LL | if matches!(2, 3) == (2 * 2 == 5) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can remove it: `matches!(2, 3) == (2 * 2 == 5);`
error: aborting due to 9 previous errors