From ef70bc15963828b7322671d2dc8ccfaa72b90608 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 28 Mar 2026 14:36:12 -0400 Subject: [PATCH] Enable cast_possible_wrap clippy lint and fix violations Replaces 14 unsigned-to-signed `as` casts with try_from().unwrap() to panic on overflow instead of silently wrapping. Co-Authored-By: Claude Opus 4.6 (1M context) --- Cargo.toml | 1 - lib/crates/fabro-api/src/server.rs | 2 +- lib/crates/fabro-cli/src/commands/run/attach.rs | 4 ++-- lib/crates/fabro-cli/src/commands/run/launcher.rs | 2 +- lib/crates/fabro-cli/src/commands/run/logs.rs | 8 ++++---- lib/crates/fabro-cli/src/commands/run/resume.rs | 2 +- lib/crates/fabro-cli/src/commands/system/prune.rs | 4 ++-- lib/crates/fabro-llm/src/providers/anthropic.rs | 2 +- lib/crates/fabro-sandbox/src/local.rs | 2 +- lib/crates/fabro-util/src/backoff.rs | 4 +++- 10 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7411f5474..ba5cd951e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,7 +88,6 @@ too_many_arguments = "allow" too_many_lines = "allow" used_underscore_binding = "allow" if_not_else = "allow" -cast_possible_wrap = "allow" cast_precision_loss = "allow" doc_markdown = "allow" # Disallowed restriction lints diff --git a/lib/crates/fabro-api/src/server.rs b/lib/crates/fabro-api/src/server.rs index 1a76c3eef..e1dfbb9c8 100644 --- a/lib/crates/fabro-api/src/server.rs +++ b/lib/crates/fabro-api/src/server.rs @@ -501,7 +501,7 @@ fn compute_queue_positions(runs: &HashMap) -> HashMap bool { #[cfg(unix)] { - unsafe { libc::kill(pid as i32, 0) == 0 } + unsafe { libc::kill(i32::try_from(pid).unwrap(), 0) == 0 } } #[cfg(not(unix))] { diff --git a/lib/crates/fabro-cli/src/commands/run/launcher.rs b/lib/crates/fabro-cli/src/commands/run/launcher.rs index da2f0c4f6..37dd85fbb 100644 --- a/lib/crates/fabro-cli/src/commands/run/launcher.rs +++ b/lib/crates/fabro-cli/src/commands/run/launcher.rs @@ -64,7 +64,7 @@ pub(crate) fn launcher_record_is_running(record: &LauncherRecord) -> bool { #[cfg(unix)] #[allow(unsafe_code)] fn process_alive(pid: u32) -> bool { - unsafe { libc::kill(pid as i32, 0) == 0 } + unsafe { libc::kill(i32::try_from(pid).unwrap(), 0) == 0 } } #[cfg(not(unix))] diff --git a/lib/crates/fabro-cli/src/commands/run/logs.rs b/lib/crates/fabro-cli/src/commands/run/logs.rs index c9aa88bea..b19ae6c1b 100644 --- a/lib/crates/fabro-cli/src/commands/run/logs.rs +++ b/lib/crates/fabro-cli/src/commands/run/logs.rs @@ -128,10 +128,10 @@ fn try_parse_relative_duration(s: &str) -> Option { let (num_str, unit) = s.split_at(s.len() - 1); let num: u64 = num_str.parse().ok()?; match unit { - "s" => Some(chrono::Duration::seconds(num as i64)), - "m" => Some(chrono::Duration::minutes(num as i64)), - "h" => Some(chrono::Duration::hours(num as i64)), - "d" => Some(chrono::Duration::days(num as i64)), + "s" => Some(chrono::Duration::seconds(i64::try_from(num).unwrap())), + "m" => Some(chrono::Duration::minutes(i64::try_from(num).unwrap())), + "h" => Some(chrono::Duration::hours(i64::try_from(num).unwrap())), + "d" => Some(chrono::Duration::days(i64::try_from(num).unwrap())), _ => None, } } diff --git a/lib/crates/fabro-cli/src/commands/run/resume.rs b/lib/crates/fabro-cli/src/commands/run/resume.rs index 7d29847c3..f750765f7 100644 --- a/lib/crates/fabro-cli/src/commands/run/resume.rs +++ b/lib/crates/fabro-cli/src/commands/run/resume.rs @@ -71,7 +71,7 @@ mod tests { fn process_alive(pid: u32) -> bool { #[cfg(unix)] { - unsafe { libc::kill(pid as i32, 0) == 0 } + unsafe { libc::kill(i32::try_from(pid).unwrap(), 0) == 0 } } #[cfg(not(unix))] { diff --git a/lib/crates/fabro-cli/src/commands/system/prune.rs b/lib/crates/fabro-cli/src/commands/system/prune.rs index c295a8054..7c54f291d 100644 --- a/lib/crates/fabro-cli/src/commands/system/prune.rs +++ b/lib/crates/fabro-cli/src/commands/system/prune.rs @@ -27,8 +27,8 @@ pub(crate) fn parse_duration(s: &str) -> Result { .parse() .with_context(|| format!("invalid duration: {s}"))?; match unit { - "h" => Ok(chrono::Duration::hours(num as i64)), - "d" => Ok(chrono::Duration::days(num as i64)), + "h" => Ok(chrono::Duration::hours(i64::try_from(num).unwrap())), + "d" => Ok(chrono::Duration::days(i64::try_from(num).unwrap())), _ => bail!("invalid duration unit '{unit}' in '{s}' (expected 'h' or 'd')"), } } diff --git a/lib/crates/fabro-llm/src/providers/anthropic.rs b/lib/crates/fabro-llm/src/providers/anthropic.rs index a54de80e4..d0caac494 100644 --- a/lib/crates/fabro-llm/src/providers/anthropic.rs +++ b/lib/crates/fabro-llm/src/providers/anthropic.rs @@ -186,7 +186,7 @@ fn estimate_reasoning_tokens(content_parts: &[ContentPart]) -> Option { }) .sum(); if total_chars > 0 { - Some((total_chars / 4).max(1) as i64) + Some(i64::try_from((total_chars / 4).max(1)).unwrap()) } else { None } diff --git a/lib/crates/fabro-sandbox/src/local.rs b/lib/crates/fabro-sandbox/src/local.rs index 6cfbbe322..9366758d2 100644 --- a/lib/crates/fabro-sandbox/src/local.rs +++ b/lib/crates/fabro-sandbox/src/local.rs @@ -488,7 +488,7 @@ async fn sigterm_then_kill(child: &mut Child) { // SAFETY: kill with a negative pid signals the entire process group. // The pid is valid because we just obtained it from child.id(). unsafe { - libc::kill(-(pid as i32), libc::SIGTERM); + libc::kill(-i32::try_from(pid).unwrap(), libc::SIGTERM); } if time::timeout(std::time::Duration::from_secs(2), child.wait()) .await diff --git a/lib/crates/fabro-util/src/backoff.rs b/lib/crates/fabro-util/src/backoff.rs index 8df4c9727..61b291d01 100644 --- a/lib/crates/fabro-util/src/backoff.rs +++ b/lib/crates/fabro-util/src/backoff.rs @@ -23,7 +23,9 @@ impl Default for BackoffPolicy { impl BackoffPolicy { pub fn delay_for_attempt(&self, attempt: u32) -> Duration { - let multiplier = self.factor.powi(attempt.saturating_sub(1) as i32); + let multiplier = self + .factor + .powi(i32::try_from(attempt.saturating_sub(1)).unwrap()); let base_delay = self.initial_delay.mul_f64(multiplier); let capped = if base_delay > self.max_delay { self.max_delay