Skip to content

Commit 3fcb7f2

Browse files
committed
docs: add docs for unchecked_duration_subtraction lint
1 parent 578f9ce commit 3fcb7f2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/docs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ docs! {
516516
"try_err",
517517
"type_complexity",
518518
"type_repetition_in_bounds",
519+
"unchecked_duration_subtraction",
519520
"undocumented_unsafe_blocks",
520521
"undropped_manually_drops",
521522
"unicode_not_nfc",
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)