From 62ab4fb9c265b9e25d61c99511cd26de0260f435 Mon Sep 17 00:00:00 2001 From: Nadir Fejzic Date: Tue, 8 Nov 2022 10:22:58 +0100 Subject: [PATCH] fix: add rust-fix annotation to unckd. duration subtr. lint test --- tests/ui/unchecked_duration_subtraction.fixed | 17 +++++++++++++++++ tests/ui/unchecked_duration_subtraction.rs | 1 + tests/ui/unchecked_duration_subtraction.stderr | 8 ++++---- 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/ui/unchecked_duration_subtraction.fixed diff --git a/tests/ui/unchecked_duration_subtraction.fixed b/tests/ui/unchecked_duration_subtraction.fixed new file mode 100644 index 000000000000..bcc231d93b16 --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.fixed @@ -0,0 +1,17 @@ +// run-rustfix +#![warn(clippy::unchecked_duration_subtraction)] + +use std::time::{Duration, Instant}; + +fn main() { + let _first = Instant::now(); + let second = Duration::from_secs(3); + + let _ = _first.checked_sub(second).unwrap();; + + let _ = Instant::now().checked_sub(Duration::from_secs(5)).unwrap();; + + let _ = _first.checked_sub(Duration::from_secs(5)).unwrap();; + + let _ = Instant::now().checked_sub(second).unwrap();; +} diff --git a/tests/ui/unchecked_duration_subtraction.rs b/tests/ui/unchecked_duration_subtraction.rs index fff1d13720d9..a14a7ea57cc5 100644 --- a/tests/ui/unchecked_duration_subtraction.rs +++ b/tests/ui/unchecked_duration_subtraction.rs @@ -1,3 +1,4 @@ +// run-rustfix #![warn(clippy::unchecked_duration_subtraction)] use std::time::{Duration, Instant}; diff --git a/tests/ui/unchecked_duration_subtraction.stderr b/tests/ui/unchecked_duration_subtraction.stderr index 6b297a01c7be..b949dbf701a0 100644 --- a/tests/ui/unchecked_duration_subtraction.stderr +++ b/tests/ui/unchecked_duration_subtraction.stderr @@ -1,5 +1,5 @@ error: unchecked subtraction of a 'Duration' from an 'Instant' - --> $DIR/unchecked_duration_subtraction.rs:9:13 + --> $DIR/unchecked_duration_subtraction.rs:10:13 | LL | let _ = _first - second; | ^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(second).unwrap();` @@ -7,19 +7,19 @@ LL | let _ = _first - second; = note: `-D clippy::unchecked-duration-subtraction` implied by `-D warnings` error: unchecked subtraction of a 'Duration' from an 'Instant' - --> $DIR/unchecked_duration_subtraction.rs:11:13 + --> $DIR/unchecked_duration_subtraction.rs:12:13 | LL | let _ = Instant::now() - Duration::from_secs(5); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap();` error: unchecked subtraction of a 'Duration' from an 'Instant' - --> $DIR/unchecked_duration_subtraction.rs:13:13 + --> $DIR/unchecked_duration_subtraction.rs:14:13 | LL | let _ = _first - Duration::from_secs(5); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap();` error: unchecked subtraction of a 'Duration' from an 'Instant' - --> $DIR/unchecked_duration_subtraction.rs:15:13 + --> $DIR/unchecked_duration_subtraction.rs:16:13 | LL | let _ = Instant::now() - second; | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap();`