Skip to content

Commit f2b5c33

Browse files
committed
fix: needless_continue wrongly unmangled macros
1 parent c86f51f commit f2b5c33

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::source::{indent_of, snippet, snippet_block, snippet_with_context};
2+
use clippy_utils::source::{indent_of, snippet_block, snippet_with_context};
33
use clippy_utils::{higher, is_from_proc_macro};
44
use rustc_ast::Label;
55
use rustc_errors::Applicability;
@@ -193,15 +193,15 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessContinue {
193193
/// - The expression node is a block with the first statement being a `continue`.
194194
fn needless_continue_in_else(else_expr: &Expr<'_>, label: Option<&Label>) -> bool {
195195
match else_expr.kind {
196-
ExprKind::Block(ref else_block, _) => is_first_block_stmt_continue(else_block, label),
196+
ExprKind::Block(else_block, _) => is_first_block_stmt_continue(else_block, label),
197197
ExprKind::Continue(l) => compare_labels(label, l.label.as_ref()),
198198
_ => false,
199199
}
200200
}
201201

202202
fn is_first_block_stmt_continue(block: &Block<'_>, label: Option<&Label>) -> bool {
203203
block.stmts.first().is_some_and(|stmt| match stmt.kind {
204-
StmtKind::Semi(ref e) | StmtKind::Expr(ref e) => {
204+
StmtKind::Semi(e) | StmtKind::Expr(e) => {
205205
if let ExprKind::Continue(ref l) = e.kind {
206206
compare_labels(label, l.label.as_ref())
207207
} else {
@@ -366,7 +366,14 @@ fn suggestion_snippet_for_continue_inside_if(cx: &LateContext<'_>, data: &LintDa
366366
}
367367

368368
fn suggestion_snippet_for_continue_inside_else(cx: &LateContext<'_>, data: &LintData<'_>) -> String {
369-
let cond_code = snippet(cx, data.if_cond.span, "..");
369+
let mut applicability = Applicability::MachineApplicable;
370+
let (cond_code, _) = snippet_with_context(
371+
cx,
372+
data.if_cond.span,
373+
data.if_expr.span.ctxt(),
374+
"..",
375+
&mut applicability,
376+
);
370377

371378
// Region B
372379
let block_code = erode_from_back(&snippet_block(cx, data.if_block.span, "..", Some(data.if_expr.span)));
@@ -402,7 +409,7 @@ fn suggestion_snippet_for_continue_inside_else(cx: &LateContext<'_>, data: &Lint
402409
}
403410
lines.join("\n")
404411
} else {
405-
"".to_string()
412+
String::new()
406413
};
407414

408415
let indent_if = indent_of(cx, data.if_expr.span).unwrap_or(0);
@@ -417,7 +424,7 @@ fn check_last_stmt_in_expr<F>(cx: &LateContext<'_>, inner_expr: &Expr<'_>, func:
417424
where
418425
F: Fn(Option<&Label>, Span),
419426
{
420-
match &inner_expr.kind {
427+
match inner_expr.kind {
421428
ExprKind::Continue(continue_label) => {
422429
func(continue_label.label.as_ref(), inner_expr.span);
423430
},
@@ -432,7 +439,7 @@ where
432439
if !match_ty.is_unit() && !match_ty.is_never() {
433440
return;
434441
}
435-
for arm in arms.iter() {
442+
for arm in arms {
436443
check_last_stmt_in_expr(cx, arm.body, func);
437444
}
438445
},

tests/ui/needless_continue.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,13 @@ fn issue14550(mut producer: impl Iterator<Item = Result<i32, u32>>) -> Result<u3
259259
};
260260
}
261261
}
262+
263+
fn issue15548() {
264+
loop {
265+
if todo!() {
266+
} else {
267+
//~^ needless_continue
268+
continue;
269+
}
270+
}
271+
}

tests/ui/needless_continue.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,21 @@ LL | | }
220220
do_something();
221221
}
222222

223-
error: aborting due to 15 previous errors
223+
error: this `else` block is redundant
224+
--> tests/ui/needless_continue.rs:266:16
225+
|
226+
LL | } else {
227+
| ________________^
228+
LL | |
229+
LL | | continue;
230+
LL | | }
231+
| |_________^
232+
|
233+
= help: consider dropping the `else` clause and merging the code that follows (in the loop) with the `if` block
234+
if todo!() {
235+
// merged code follows:
236+
237+
}
238+
239+
error: aborting due to 16 previous errors
224240

0 commit comments

Comments
 (0)