1
1
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:: macros:: root_macro_call_first_node;
3
+ use clippy_utils:: source:: { indent_of, snippet_block, snippet_with_context} ;
3
4
use clippy_utils:: { higher, is_from_proc_macro} ;
4
5
use rustc_ast:: Label ;
5
6
use rustc_errors:: Applicability ;
@@ -132,7 +133,12 @@ declare_lint_pass!(NeedlessContinue => [NEEDLESS_CONTINUE]);
132
133
133
134
impl < ' tcx > LateLintPass < ' tcx > for NeedlessContinue {
134
135
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
135
- if !expr. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) && !is_from_proc_macro ( cx, expr) {
136
+ // We cannot use `from_expansion` because for loops, while loops and while let loops are desugared
137
+ // into `loop` expressions.
138
+ if !expr. span . in_external_macro ( cx. sess ( ) . source_map ( ) )
139
+ && !is_from_proc_macro ( cx, expr)
140
+ && root_macro_call_first_node ( cx, expr) . is_none ( )
141
+ {
136
142
check_and_warn ( cx, expr) ;
137
143
}
138
144
}
@@ -193,15 +199,15 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessContinue {
193
199
/// - The expression node is a block with the first statement being a `continue`.
194
200
fn needless_continue_in_else ( else_expr : & Expr < ' _ > , label : Option < & Label > ) -> bool {
195
201
match else_expr. kind {
196
- ExprKind :: Block ( ref else_block, _) => is_first_block_stmt_continue ( else_block, label) ,
202
+ ExprKind :: Block ( else_block, _) => is_first_block_stmt_continue ( else_block, label) ,
197
203
ExprKind :: Continue ( l) => compare_labels ( label, l. label . as_ref ( ) ) ,
198
204
_ => false ,
199
205
}
200
206
}
201
207
202
208
fn is_first_block_stmt_continue ( block : & Block < ' _ > , label : Option < & Label > ) -> bool {
203
209
block. stmts . first ( ) . is_some_and ( |stmt| match stmt. kind {
204
- StmtKind :: Semi ( ref e) | StmtKind :: Expr ( ref e) => {
210
+ StmtKind :: Semi ( e) | StmtKind :: Expr ( e) => {
205
211
if let ExprKind :: Continue ( ref l) = e. kind {
206
212
compare_labels ( label, l. label . as_ref ( ) )
207
213
} else {
@@ -366,7 +372,14 @@ fn suggestion_snippet_for_continue_inside_if(cx: &LateContext<'_>, data: &LintDa
366
372
}
367
373
368
374
fn suggestion_snippet_for_continue_inside_else ( cx : & LateContext < ' _ > , data : & LintData < ' _ > ) -> String {
369
- let cond_code = snippet ( cx, data. if_cond . span , ".." ) ;
375
+ let mut applicability = Applicability :: MachineApplicable ;
376
+ let ( cond_code, _) = snippet_with_context (
377
+ cx,
378
+ data. if_cond . span ,
379
+ data. if_expr . span . ctxt ( ) ,
380
+ ".." ,
381
+ & mut applicability,
382
+ ) ;
370
383
371
384
// Region B
372
385
let block_code = erode_from_back ( & snippet_block ( cx, data. if_block . span , ".." , Some ( data. if_expr . span ) ) ) ;
@@ -402,7 +415,7 @@ fn suggestion_snippet_for_continue_inside_else(cx: &LateContext<'_>, data: &Lint
402
415
}
403
416
lines. join ( "\n " )
404
417
} else {
405
- "" . to_string ( )
418
+ String :: new ( )
406
419
} ;
407
420
408
421
let indent_if = indent_of ( cx, data. if_expr . span ) . unwrap_or ( 0 ) ;
@@ -417,7 +430,7 @@ fn check_last_stmt_in_expr<F>(cx: &LateContext<'_>, inner_expr: &Expr<'_>, func:
417
430
where
418
431
F : Fn ( Option < & Label > , Span ) ,
419
432
{
420
- match & inner_expr. kind {
433
+ match inner_expr. kind {
421
434
ExprKind :: Continue ( continue_label) => {
422
435
func ( continue_label. label . as_ref ( ) , inner_expr. span ) ;
423
436
} ,
@@ -432,7 +445,7 @@ where
432
445
if !match_ty. is_unit ( ) && !match_ty. is_never ( ) {
433
446
return ;
434
447
}
435
- for arm in arms. iter ( ) {
448
+ for arm in arms {
436
449
check_last_stmt_in_expr ( cx, arm. body , func) ;
437
450
}
438
451
} ,
0 commit comments