Skip to content

Commit 3b4e42b

Browse files
committed
test: add tests for 'unchecked_duration_subtraction' lint
1 parent 708c2d9 commit 3b4e42b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::unchecked_duration_subtraction)]
2+
3+
use std::time::{Duration, Instant};
4+
5+
fn main() {
6+
let _first = Instant::now();
7+
let second = Duration::from_secs(3);
8+
9+
let _ = _first - second;
10+
11+
let _ = Instant::now() - Duration::from_secs(5);
12+
13+
let _ = _first - Duration::from_secs(5);
14+
15+
let _ = Instant::now() - second;
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant
2+
error: unchecked subtraction of a 'Duration' from an 'Instant'
3+
--> $DIR/unchecked_duration_subtraction.rs:9:13
4+
|
5+
LL | let _ = _first - second;
6+
| ^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(second).unwrap();`
7+
|
8+
= note: `-D clippy::unchecked-duration-subtraction` implied by `-D warnings`
9+
10+
[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant
11+
error: unchecked subtraction of a 'Duration' from an 'Instant'
12+
--> $DIR/unchecked_duration_subtraction.rs:11:13
13+
|
14+
LL | let _ = Instant::now() - Duration::from_secs(5);
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap();`
16+
17+
[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant
18+
error: unchecked subtraction of a 'Duration' from an 'Instant'
19+
--> $DIR/unchecked_duration_subtraction.rs:13:13
20+
|
21+
LL | let _ = _first - Duration::from_secs(5);
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap();`
23+
24+
[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant
25+
error: unchecked subtraction of a 'Duration' from an 'Instant'
26+
--> $DIR/unchecked_duration_subtraction.rs:15:13
27+
|
28+
LL | let _ = Instant::now() - second;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap();`
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)