From af6816c5b6a4b2cca1308d52c40543bb845b9d44 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sun, 6 Oct 2024 18:33:39 +0200 Subject: [PATCH] `infinite_loop`: do not defensively use `saturating_sub()` Using `saturating_sub()` here in code which cannot fail brings a false sense of security. If for any reason a logic error was introduced and caused `self.loop_depth` to reach 0 before being decremented, using `saturating_sub(1)` would silently mask the programming error instead of panicking loudly as it should (at least in dev profile). --- clippy_lints/src/loops/infinite_loop.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/loops/infinite_loop.rs b/clippy_lints/src/loops/infinite_loop.rs index 858e3be5093ef..64ae080701528 100644 --- a/clippy_lints/src/loops/infinite_loop.rs +++ b/clippy_lints/src/loops/infinite_loop.rs @@ -112,7 +112,7 @@ impl<'hir> Visitor<'hir> for LoopVisitor<'hir, '_> { ExprKind::Loop(..) => { self.loop_depth += 1; walk_expr(self, ex); - self.loop_depth = self.loop_depth.saturating_sub(1); + self.loop_depth -= 1; }, _ => { // Calls to a function that never return