docs: add docs for unchecked_duration_subtraction
lint
This commit is contained in:
parent
3b4e42b91b
commit
b280dbe5f7
19
src/docs/unchecked_duration_subtraction.txt
Normal file
19
src/docs/unchecked_duration_subtraction.txt
Normal file
@ -0,0 +1,19 @@
|
||||
### What it does
|
||||
Finds patterns of unchecked subtraction of [`Duration`] from [`Instant::now()`].
|
||||
|
||||
### Why is this bad?
|
||||
Unchecked subtraction could cause underflow on certain platforms, leading to bugs and/or
|
||||
unintentional panics.
|
||||
|
||||
### Example
|
||||
```
|
||||
let time_passed = Instant::now() - Duration::from_secs(5);
|
||||
```
|
||||
|
||||
Use instead:
|
||||
```
|
||||
let time_passed = Instant::now().checked_sub(Duration::from_secs(5));
|
||||
```
|
||||
|
||||
[`Duration`]: std::time::Duration
|
||||
[`Instant::now()`]: std::time::Instant::now;
|
Loading…
Reference in New Issue
Block a user