Skip to content

Commit 27d02cc

Browse files
committed
fix: if_not_else wrong unmangled macros
1 parent 0226fa9 commit 27d02cc

File tree

4 files changed

+100
-16
lines changed

4 files changed

+100
-16
lines changed

clippy_lints/src/if_not_else.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::consts::is_zero_integer_const;
22
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
33
use clippy_utils::is_else_clause;
4-
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet};
4+
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet_with_context};
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -78,15 +78,24 @@ impl LateLintPass<'_> for IfNotElse {
7878
// }
7979
// ```
8080
if !e.span.from_expansion() && !is_else_clause(cx.tcx, e) {
81+
let mut applicability = Applicability::MachineApplicable;
8182
match cond.kind {
8283
ExprKind::Unary(UnOp::Not, _) | ExprKind::Binary(_, _, _) => span_lint_and_sugg(
8384
cx,
8485
IF_NOT_ELSE,
8586
e.span,
8687
msg,
8788
"try",
88-
make_sugg(cx, &cond.kind, cond_inner.span, els.span, "..", Some(e.span)),
89-
Applicability::MachineApplicable,
89+
make_sugg(
90+
cx,
91+
e.span,
92+
&cond.kind,
93+
cond_inner.span,
94+
els.span,
95+
"..",
96+
&mut applicability,
97+
),
98+
applicability,
9099
),
91100
_ => span_lint_and_help(cx, IF_NOT_ELSE, e.span, msg, None, help),
92101
}
@@ -95,30 +104,29 @@ impl LateLintPass<'_> for IfNotElse {
95104
}
96105
}
97106

107+
#[expect(clippy::too_many_arguments)]
98108
fn make_sugg<'a>(
99109
sess: &impl HasSession,
110+
expr_span: Span,
100111
cond_kind: &'a ExprKind<'a>,
101112
cond_inner: Span,
102113
els_span: Span,
103114
default: &'a str,
104-
indent_relative_to: Option<Span>,
115+
applicability: &mut Applicability,
105116
) -> String {
106-
let cond_inner_snip = snippet(sess, cond_inner, default);
107-
let els_snip = snippet(sess, els_span, default);
108-
let indent = indent_relative_to.and_then(|s| indent_of(sess, s));
117+
let (cond_inner_snip, _) = snippet_with_context(sess, cond_inner, expr_span.ctxt(), default, applicability);
118+
let (els_snip, _) = snippet_with_context(sess, els_span, expr_span.ctxt(), default, applicability);
119+
let indent = indent_of(sess, expr_span);
109120

110121
let suggestion = match cond_kind {
111122
ExprKind::Unary(UnOp::Not, cond_rest) => {
112-
format!(
113-
"if {} {} else {}",
114-
snippet(sess, cond_rest.span, default),
115-
els_snip,
116-
cond_inner_snip
117-
)
123+
let (cond_rest_snip, _) =
124+
snippet_with_context(sess, cond_rest.span, expr_span.ctxt(), default, applicability);
125+
format!("if {cond_rest_snip} {els_snip} else {cond_inner_snip}")
118126
},
119127
ExprKind::Binary(_, lhs, rhs) => {
120-
let lhs_snip = snippet(sess, lhs.span, default);
121-
let rhs_snip = snippet(sess, rhs.span, default);
128+
let (lhs_snip, _) = snippet_with_context(sess, lhs.span, expr_span.ctxt(), default, applicability);
129+
let (rhs_snip, _) = snippet_with_context(sess, rhs.span, expr_span.ctxt(), default, applicability);
122130

123131
format!("if {lhs_snip} == {rhs_snip} {els_snip} else {cond_inner_snip}")
124132
},

tests/ui/if_not_else.fixed

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,20 @@ fn with_annotations() {
7676
println!("foo is false");
7777
}
7878
}
79+
80+
fn issue15924() {
81+
let x = 0;
82+
if matches!(x, 0..10) {
83+
println!(":(");
84+
} else {
85+
//~^ if_not_else
86+
println!(":)");
87+
}
88+
89+
if dbg!(x) == 1 {
90+
println!(":(");
91+
} else {
92+
//~^ if_not_else
93+
println!(":)");
94+
}
95+
}

tests/ui/if_not_else.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,20 @@ fn with_annotations() {
7676
println!("foo"); /* foo */
7777
}
7878
}
79+
80+
fn issue15924() {
81+
let x = 0;
82+
if !matches!(x, 0..10) {
83+
//~^ if_not_else
84+
println!(":)");
85+
} else {
86+
println!(":(");
87+
}
88+
89+
if dbg!(x) != 1 {
90+
//~^ if_not_else
91+
println!(":)");
92+
} else {
93+
println!(":(");
94+
}
95+
}

tests/ui/if_not_else.stderr

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,47 @@ LL + println!("foo is false");
147147
LL + }
148148
|
149149

150-
error: aborting due to 6 previous errors
150+
error: unnecessary boolean `not` operation
151+
--> tests/ui/if_not_else.rs:82:5
152+
|
153+
LL | / if !matches!(x, 0..10) {
154+
LL | |
155+
LL | | println!(":)");
156+
LL | | } else {
157+
LL | | println!(":(");
158+
LL | | }
159+
| |_____^
160+
|
161+
help: try
162+
|
163+
LL ~ if matches!(x, 0..10) {
164+
LL + println!(":(");
165+
LL + } else {
166+
LL +
167+
LL + println!(":)");
168+
LL + }
169+
|
170+
171+
error: unnecessary `!=` operation
172+
--> tests/ui/if_not_else.rs:89:5
173+
|
174+
LL | / if dbg!(x) != 1 {
175+
LL | |
176+
LL | | println!(":)");
177+
LL | | } else {
178+
LL | | println!(":(");
179+
LL | | }
180+
| |_____^
181+
|
182+
help: try
183+
|
184+
LL ~ if dbg!(x) == 1 {
185+
LL + println!(":(");
186+
LL + } else {
187+
LL +
188+
LL + println!(":)");
189+
LL + }
190+
|
191+
192+
error: aborting due to 8 previous errors
151193

0 commit comments

Comments
 (0)