Skip to content

Commit d4e30b5

Browse files
martinthomsonNot-Nik
authored andcommitted
Deny use of Instant::now() (#3522)
Except where we have good reason. Also, fix some lints on linux that we haven't been checking properly.
1 parent 587977a commit d4e30b5

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

.clippy.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
disallowed-methods = [
2-
{ path = "std::slice::from_raw_parts", reason = "see null_safe_slice" }
2+
{ path = "std::slice::from_raw_parts", reason = "see null_safe_slice" },
3+
{ path = "std::time::Instant::now", reason = "time should be provided externally" }
34
]
45
disallowed-macros = [
56
{ path = "std::dbg" }

src/time.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl TimeZero {
4949
/// To avoid that, we make sure that this sets the base time using the first value
5050
/// it sees if it is in the past. If it is not, then use `Instant::now()` instead.
5151
pub fn baseline(t: Instant) -> Self {
52+
#[expect(
53+
clippy::disallowed_methods,
54+
reason = "Special handling for NSPR time conversion"
55+
)]
5256
let now = Instant::now();
5357
let prnow = unsafe { PR_Now() };
5458

@@ -75,6 +79,10 @@ static BASE_TIME: OnceCell<TimeZero> = OnceCell::new();
7579

7680
fn get_base() -> &'static TimeZero {
7781
BASE_TIME.get_or_init(|| TimeZero {
82+
#[expect(
83+
clippy::disallowed_methods,
84+
reason = "Special handling for NSPR time conversion"
85+
)]
7886
instant: Instant::now(),
7987
prtime: unsafe { PR_Now() },
8088
})
@@ -256,13 +264,15 @@ mod test {
256264
}
257265

258266
#[test]
267+
#[expect(clippy::disallowed_methods, reason = "Test for special time handling")]
259268
fn timezero_baseline_future() {
260269
let tz = TimeZero::baseline(Instant::now() + Duration::from_secs(10));
261270
let now = Instant::now();
262271
assert!(tz.instant <= now && tz.instant + Duration::from_millis(100) > now);
263272
}
264273

265274
#[test]
275+
#[expect(clippy::disallowed_methods, reason = "Test for special time handling")]
266276
fn timezero_baseline_past() {
267277
let past = Instant::now().checked_sub(Duration::from_secs(5)).unwrap();
268278
assert_eq!(TimeZero::baseline(past).instant, past);

0 commit comments

Comments
 (0)