diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-05-08 11:23:46 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2025-05-08 13:26:50 +0200 |
| commit | 38d2387f691df3554a7ac6de2645711c85becb85 (patch) | |
| tree | 61b7c5ca6c06510adaf011ae4ae92ebf06baa59e | |
| parent | f88f9a9dc5bb494814ba63634a83efac83769d3d (diff) | |
| download | rust-38d2387f691df3554a7ac6de2645711c85becb85.tar.gz rust-38d2387f691df3554a7ac6de2645711c85becb85.zip | |
Simplify `is_simple_break_expr`
This is shorter, and also avoids overloading the `peel_blocks()` from `clippy_utils` with different semantics.
| -rw-r--r-- | clippy_lints/src/loops/while_let_loop.rs | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/clippy_lints/src/loops/while_let_loop.rs b/clippy_lints/src/loops/while_let_loop.rs index bd04827a1f0..0032dc3d7a4 100644 --- a/clippy_lints/src/loops/while_let_loop.rs +++ b/clippy_lints/src/loops/while_let_loop.rs @@ -42,27 +42,17 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_blo } } -/// Returns `true` if expr contains a single break expression without a label or eub-expression. +/// Returns `true` if expr contains a single break expression without a label or sub-expression, +/// possibly embedded in blocks. fn is_simple_break_expr(e: &Expr<'_>) -> bool { - matches!(peel_blocks(e).kind, ExprKind::Break(dest, None) if dest.label.is_none()) -} - -/// Removes any blocks containing only a single expression. -fn peel_blocks<'tcx>(e: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> { if let ExprKind::Block(b, _) = e.kind { match (b.stmts, b.expr) { - ([s], None) => { - if let StmtKind::Expr(e) | StmtKind::Semi(e) = s.kind { - peel_blocks(e) - } else { - e - } - }, - ([], Some(e)) => peel_blocks(e), - _ => e, + ([s], None) => matches!(s.kind, StmtKind::Expr(e) | StmtKind::Semi(e) if is_simple_break_expr(e)), + ([], Some(e)) => is_simple_break_expr(e), + _ => false, } } else { - e + matches!(e.kind, ExprKind::Break(dest, None) if dest.label.is_none()) } } |
