mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
docs: add docs for unchecked_duration_subtraction
lint
This commit is contained in:
parent
3b4e42b91b
commit
b280dbe5f7
1 changed files with 19 additions and 0 deletions
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 a new issue