Skip to content

Commit b280dbe

Browse files
committed
docs: add docs for unchecked_duration_subtraction lint
1 parent 3b4e42b commit b280dbe

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### What it does
2+
Finds patterns of unchecked subtraction of [`Duration`] from [`Instant::now()`].
3+
4+
### Why is this bad?
5+
Unchecked subtraction could cause underflow on certain platforms, leading to bugs and/or
6+
unintentional panics.
7+
8+
### Example
9+
```
10+
let time_passed = Instant::now() - Duration::from_secs(5);
11+
```
12+
13+
Use instead:
14+
```
15+
let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
16+
```
17+
18+
[`Duration`]: std::time::Duration
19+
[`Instant::now()`]: std::time::Instant::now;

0 commit comments

Comments
 (0)