From 505ea68781724a19e041b6a1f344ab1ed65fd83a Mon Sep 17 00:00:00 2001 From: Daniel Szoke Date: Wed, 6 May 2026 18:06:23 +0200 Subject: [PATCH] fix: Enable `clippy::arithmetic_side_effects` lint [This lint](https://rust-lang.github.io/rust-clippy/master/index.html?search=clippy%3A%3Aarithmetic_side_effects#arithmetic_side_effects) warns against using normal math operations, like `+`, `-`, etc. In Rust, these operators panic on overflow in debug builds and overflow in release builds. Enabling this lint forces us to handle potential overflow explicitly. This change enables the lint and fixes the easy violations. Three follow up PRs will address the more complex cases. Ref #1113 Ref [RUST-211](https://linear.app/getsentry/issue/RUST-211/enforce-checked-arithmetic-in-the-sdk) --- Cargo.toml | 3 +++ sentry-contexts/src/utils.rs | 2 +- sentry-core/src/logger.rs | 4 ++-- sentry-core/src/session.rs | 2 ++ sentry-types/src/protocol/envelope.rs | 2 ++ sentry/src/transports/ratelimit.rs | 2 ++ 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a4edd0eb..f1fdc317a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,5 +24,8 @@ homepage = "https://sentry.io/welcome/" edition = "2021" rust-version = "1.88" +[workspace.lints.clippy] +arithmetic-side-effects.level = "warn" + [workspace.lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(doc_cfg)'] } diff --git a/sentry-contexts/src/utils.rs b/sentry-contexts/src/utils.rs index 8acd9c83b..6c311a36e 100644 --- a/sentry-contexts/src/utils.rs +++ b/sentry-contexts/src/utils.rs @@ -54,7 +54,7 @@ mod model_support { pub fn get_macos_version() -> Option { let version = sysctlbyname_call("kern.osproductversion")?; - let dot_count = version.split('.').count() - 1; + let dot_count = version.split('.').count().saturating_sub(1); if dot_count < 2 { return Some(version + ".0"); } diff --git a/sentry-core/src/logger.rs b/sentry-core/src/logger.rs index 293bf3740..0a0e4db66 100644 --- a/sentry-core/src/logger.rs +++ b/sentry-core/src/logger.rs @@ -25,13 +25,13 @@ macro_rules! logger_log { "sentry.message.template".to_owned(), $crate::protocol::LogAttribute($crate::protocol::Value::from($fmt)) ); - let mut i = 0; + let mut i = 0usize; $( attributes.insert( format!("sentry.message.parameter.{}", i), $crate::protocol::LogAttribute($crate::protocol::Value::from($arg)) ); - i += 1; + i = i.checked_add(1).expect("number of parametrs should not overflow usize"); )* let _ = i; // avoid triggering the `unused_assignments` lint diff --git a/sentry-core/src/session.rs b/sentry-core/src/session.rs index e9fd2f298..baa8a22d5 100644 --- a/sentry-core/src/session.rs +++ b/sentry-core/src/session.rs @@ -2,6 +2,8 @@ //! //! +#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1117 + #[cfg(feature = "release-health")] pub use session_impl::*; diff --git a/sentry-types/src/protocol/envelope.rs b/sentry-types/src/protocol/envelope.rs index 9ce4c5610..0d2c28823 100644 --- a/sentry-types/src/protocol/envelope.rs +++ b/sentry-types/src/protocol/envelope.rs @@ -1,3 +1,5 @@ +#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1118 + use std::{borrow::Cow, io::Write, path::Path, time::SystemTime}; use serde::{Deserialize, Serialize}; diff --git a/sentry/src/transports/ratelimit.rs b/sentry/src/transports/ratelimit.rs index b47f9e533..2a9803197 100644 --- a/sentry/src/transports/ratelimit.rs +++ b/sentry/src/transports/ratelimit.rs @@ -1,3 +1,5 @@ +#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1115 + use httpdate::parse_http_date; use std::time::{Duration, SystemTime};