Skip to content

Commit d65272b

Browse files
fix: Enable clippy::arithmetic_side_effects lint (#1119)
[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)
1 parent f801a2c commit d65272b

6 files changed

Lines changed: 12 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ homepage = "https://sentry.io/welcome/"
2424
edition = "2021"
2525
rust-version = "1.88"
2626

27+
[workspace.lints.clippy]
28+
arithmetic-side-effects.level = "warn"
29+
2730
[workspace.lints.rust]
2831
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(doc_cfg)'] }

sentry-contexts/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mod model_support {
5454

5555
pub fn get_macos_version() -> Option<String> {
5656
let version = sysctlbyname_call("kern.osproductversion")?;
57-
let dot_count = version.split('.').count() - 1;
57+
let dot_count = version.split('.').count().saturating_sub(1);
5858
if dot_count < 2 {
5959
return Some(version + ".0");
6060
}

sentry-core/src/logger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ macro_rules! logger_log {
2525
"sentry.message.template".to_owned(),
2626
$crate::protocol::LogAttribute($crate::protocol::Value::from($fmt))
2727
);
28-
let mut i = 0;
28+
let mut i = 0usize;
2929
$(
3030
attributes.insert(
3131
format!("sentry.message.parameter.{}", i),
3232
$crate::protocol::LogAttribute($crate::protocol::Value::from($arg))
3333
);
34-
i += 1;
34+
i = i.checked_add(1).expect("number of parametrs should not overflow usize");
3535
)*
3636
let _ = i; // avoid triggering the `unused_assignments` lint
3737

sentry-core/src/session.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! <https://develop.sentry.dev/sdk/sessions/>
44
5+
#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1117
6+
57
#[cfg(feature = "release-health")]
68
pub use session_impl::*;
79

sentry-types/src/protocol/envelope.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1118
2+
13
use std::{borrow::Cow, io::Write, path::Path, time::SystemTime};
24

35
use serde::{Deserialize, Serialize};

sentry/src/transports/ratelimit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(clippy::arithmetic_side_effects)] // https://github.com/getsentry/sentry-rust/issues/1115
2+
13
use httpdate::parse_http_date;
24
use std::time::{Duration, SystemTime};
35

0 commit comments

Comments
 (0)