diff options
| author | Nadir Fejzic <nadirfejzo@gmail.com> | 2022-10-01 16:02:57 +0200 |
|---|---|---|
| committer | Nadir Fejzic <nadirfejzo@gmail.com> | 2022-11-10 15:46:41 +0100 |
| commit | 3b4e42b91b0df11c43983c6fe67bb971f472aa3f (patch) | |
| tree | 8c37f47f219744339d38bceccdf4ba00c41519a1 | |
| parent | 708c2d95c14829e0acbe7f40d9bea39e7bb03450 (diff) | |
test: add tests for 'unchecked_duration_subtraction' lint
| -rw-r--r-- | tests/ui/unchecked_duration_subtraction.rs | 16 | ||||
| -rw-r--r-- | tests/ui/unchecked_duration_subtraction.stderr | 32 |
2 files changed, 48 insertions, 0 deletions
diff --git a/tests/ui/unchecked_duration_subtraction.rs b/tests/ui/unchecked_duration_subtraction.rs new file mode 100644 index 00000000000..fff1d13720d --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.rs @@ -0,0 +1,16 @@ +#![warn(clippy::unchecked_duration_subtraction)] + +use std::time::{Duration, Instant}; + +fn main() { + let _first = Instant::now(); + let second = Duration::from_secs(3); + + let _ = _first - second; + + let _ = Instant::now() - Duration::from_secs(5); + + let _ = _first - Duration::from_secs(5); + + let _ = Instant::now() - second; +} diff --git a/tests/ui/unchecked_duration_subtraction.stderr b/tests/ui/unchecked_duration_subtraction.stderr new file mode 100644 index 00000000000..827b18a5a09 --- /dev/null +++ b/tests/ui/unchecked_duration_subtraction.stderr @@ -0,0 +1,32 @@ +[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:9:13 + | +LL | let _ = _first - second; + | ^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(second).unwrap();` + | + = note: `-D clippy::unchecked-duration-subtraction` implied by `-D warnings` + +[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:11:13 + | +LL | let _ = Instant::now() - Duration::from_secs(5); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(Duration::from_secs(5)).unwrap();` + +[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:13:13 + | +LL | let _ = _first - Duration::from_secs(5); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `_first.checked_sub(Duration::from_secs(5)).unwrap();` + +[clippy_lints/src/unchecked_duration_subtraction.rs:75] def = std::time::Instant +error: unchecked subtraction of a 'Duration' from an 'Instant' + --> $DIR/unchecked_duration_subtraction.rs:15:13 + | +LL | let _ = Instant::now() - second; + | ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Instant::now().checked_sub(second).unwrap();` + +error: aborting due to 4 previous errors + |
