Skip to content

Commit adcc02e

Browse files
author
Michael Wright
committed
Address reviews
1 parent 0579c3e commit adcc02e

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

clippy_lints/src/loops.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
488488
},
489489
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
490490
}
491-
};
491+
}
492492

493493
// check for `loop { if let {} else break }` that could be `while let`
494494
// (also matches an explicit "match" instead of "if let")
@@ -587,16 +587,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
587587
}
588588
}
589589

590-
if_chain! {
591-
if let ExprKind::Loop(block, _, LoopSource::While) = &expr.node;
592-
if let Block { expr: Some(expr), .. } = &**block;
593-
if let ExprKind::Match(cond, arms, MatchSource::WhileDesugar) = &expr.node;
594-
if let ExprKind::DropTemps(cond) = &cond.node;
595-
if let [arm, ..] = &arms[..];
596-
if let Arm { body, .. } = arm;
597-
then {
598-
check_infinite_loop(cx, cond, body);
599-
}
590+
if let Some((cond, body)) = higher::while_loop(&expr) {
591+
check_infinite_loop(cx, cond, body);
600592
}
601593

602594
check_needless_collect(expr, cx);

clippy_lints/src/utils/author.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ fn loop_desugaring_name(des: hir::LoopSource) -> &'static str {
702702
match des {
703703
hir::LoopSource::ForLoop => "LoopSource::ForLoop",
704704
hir::LoopSource::Loop => "LoopSource::Loop",
705-
hir::LoopSource::While => "LoopSource::WhileDesugar",
705+
hir::LoopSource::While => "LoopSource::While",
706706
hir::LoopSource::WhileLet => "LoopSource::WhileLet",
707707
}
708708
}

clippy_lints/src/utils/higher.rs

+17
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ pub fn for_loop(expr: &hir::Expr) -> Option<(&hir::Pat, &hir::Expr, &hir::Expr)>
199199
None
200200
}
201201

202+
/// Recover the essential nodes of a desugared while loop:
203+
/// `while cond { body }` becomes `(cond, body)`.
204+
pub fn while_loop(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr)> {
205+
if_chain! {
206+
if let hir::ExprKind::Loop(block, _, hir::LoopSource::While) = &expr.node;
207+
if let hir::Block { expr: Some(expr), .. } = &**block;
208+
if let hir::ExprKind::Match(cond, arms, hir::MatchSource::WhileDesugar) = &expr.node;
209+
if let hir::ExprKind::DropTemps(cond) = &cond.node;
210+
if let [arm, ..] = &arms[..];
211+
if let hir::Arm { body, .. } = arm;
212+
then {
213+
return Some((cond, body));
214+
}
215+
}
216+
None
217+
}
218+
202219
/// Recover the essential nodes of a desugared if block
203220
/// `if cond { then } else { els }` becomes `(cond, then, Some(els))`
204221
pub fn if_block(expr: &hir::Expr) -> Option<(&hir::Expr, &hir::Expr, Option<&hir::Expr>)> {

0 commit comments

Comments
 (0)