From e30cae5c7fcc7120f0fbba720655732a0606f09f Mon Sep 17 00:00:00 2001 From: Damocles Date: Fri, 17 Apr 2026 23:28:24 +0200 Subject: [PATCH] enable clippy pedantic for nova-stats, fix all warnings --- stats-daemon/Cargo.toml | 6 ++++++ stats-daemon/src/cpu.rs | 6 +----- stats-daemon/src/gpu.rs | 8 +++----- stats-daemon/src/main.rs | 4 ++-- stats-daemon/src/temp.rs | 8 +++----- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/stats-daemon/Cargo.toml b/stats-daemon/Cargo.toml index 71f41fe..2e42faf 100644 --- a/stats-daemon/Cargo.toml +++ b/stats-daemon/Cargo.toml @@ -3,6 +3,12 @@ name = "nova-stats" version = "0.1.0" edition = "2021" +[lints.clippy] +pedantic = { level = "warn", priority = -1 } +cast_possible_truncation = "allow" +cast_precision_loss = "allow" +cast_sign_loss = "allow" + [[bin]] name = "nova-stats" path = "src/main.rs" diff --git a/stats-daemon/src/cpu.rs b/stats-daemon/src/cpu.rs index 34c8863..c0ad4ae 100644 --- a/stats-daemon/src/cpu.rs +++ b/stats-daemon/src/cpu.rs @@ -56,11 +56,7 @@ pub fn emit_cpu(out: &mut impl Write, prev: &[Sample], curr: &[Sample], freqs: & return; } - let usage = prev - .first() - .zip(curr.first()) - .map(|(p, c)| pct(p, c)) - .unwrap_or(0); + let usage = prev.first().zip(curr.first()).map_or(0, |(p, c)| pct(p, c)); let core_usage: Vec = prev .iter() diff --git a/stats-daemon/src/gpu.rs b/stats-daemon/src/gpu.rs index 3c2a989..5d114b9 100644 --- a/stats-daemon/src/gpu.rs +++ b/stats-daemon/src/gpu.rs @@ -53,7 +53,7 @@ fn find_amd_hwmon() -> Option { None } -fn read_amd(card: &str, hwmon: &Option) -> Option { +fn read_amd(card: &str, hwmon: Option<&String>) -> Option { let usage: u32 = fs::read_to_string(format!("{card}/gpu_busy_percent")) .ok()? .trim() @@ -70,11 +70,9 @@ fn read_amd(card: &str, hwmon: &Option) -> Option { .parse() .ok()?; let temp_c = hwmon - .as_ref() .and_then(|h| fs::read_to_string(format!("{h}/temp1_input")).ok()) .and_then(|s| s.trim().parse::().ok()) - .map(|mc| mc / 1000) - .unwrap_or(0); + .map_or(0, |mc| mc / 1000); Some(GpuInfo { usage, vram_used_gb: vram_used as f64 / 1_073_741_824.0, @@ -114,7 +112,7 @@ pub fn emit_gpu(out: &mut impl Write, backend: &GpuBackend) { GpuBackend::Amd { card_path, hwmon_path, - } => read_amd(card_path, hwmon_path), + } => read_amd(card_path, hwmon_path.as_ref()), GpuBackend::Nvidia => read_nvidia(), GpuBackend::None => return, }; diff --git a/stats-daemon/src/main.rs b/stats-daemon/src/main.rs index 572b864..66fa98d 100644 --- a/stats-daemon/src/main.rs +++ b/stats-daemon/src/main.rs @@ -55,8 +55,8 @@ fn main() { tick += 1; let elapsed = t0.elapsed(); - if elapsed < interval { - thread::sleep(interval - elapsed); + if let Some(remaining) = interval.checked_sub(elapsed) { + thread::sleep(remaining); } } } diff --git a/stats-daemon/src/temp.rs b/stats-daemon/src/temp.rs index 2c93ef9..e84765c 100644 --- a/stats-daemon/src/temp.rs +++ b/stats-daemon/src/temp.rs @@ -15,9 +15,8 @@ pub fn read_thermal_devices() -> Vec { let temp_path = format!("/sys/class/thermal/thermal_zone{i}/temp"); let type_path = format!("/sys/class/thermal/thermal_zone{i}/type"); - let temp_str = match fs::read_to_string(&temp_path) { - Ok(s) => s, - Err(_) => break, + let Ok(temp_str) = fs::read_to_string(&temp_path) else { + break; }; let millic: i32 = match temp_str.trim().parse() { @@ -27,8 +26,7 @@ pub fn read_thermal_devices() -> Vec { let celsius = millic / 1000; let name = fs::read_to_string(&type_path) - .map(|s| s.trim().to_string()) - .unwrap_or_else(|_| format!("zone{i}")); + .map_or_else(|_| format!("zone{i}"), |s| s.trim().to_string()); // Keep the highest temp seen for each device type let entry = by_name.entry(name).or_insert(celsius);