From ee0de538d4196596986328311bb097a07f473273 Mon Sep 17 00:00:00 2001 From: bluthej Date: Thu, 23 Mar 2023 22:29:30 +0100 Subject: [PATCH] Add some tests --- tests/ui/clear_with_drain.fixed | 48 ++++++++++++++++++++++++++------ tests/ui/clear_with_drain.rs | 48 ++++++++++++++++++++++++++------ tests/ui/clear_with_drain.stderr | 8 +++--- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/tests/ui/clear_with_drain.fixed b/tests/ui/clear_with_drain.fixed index 62b0af08eea..6ab05d9baca 100644 --- a/tests/ui/clear_with_drain.fixed +++ b/tests/ui/clear_with_drain.fixed @@ -3,38 +3,70 @@ #![warn(clippy::clear_with_drain)] fn range() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(0..u.len()); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(0..v.len()); // Yay + + let mut v = vec![1, 2, 3]; + let n = v.drain(0..v.len()).count(); // Yay + + let mut v = vec![1, 2, 3]; v.clear(); // Nay } fn range_from() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(0..); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(0..); // Yay + + let mut v = vec![1, 2, 3]; + let next = v.drain(0..).next(); // Yay + + let mut v = vec![1, 2, 3]; v.clear(); // Nay } fn range_full() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(..); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(..); // Yay + + let mut v = vec![1, 2, 3]; + // Yay + for x in v.drain(..) { + let y = format!("x = {x}"); + } + + let mut v = vec![1, 2, 3]; v.clear(); // Nay } fn range_to() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(..u.len()); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(..v.len()); // Yay + + let mut v = vec![1, 2, 3]; + // Yay + for x in v.drain(..v.len()) { + let y = format!("x = {x}"); + } + + let mut v = vec![1, 2, 3]; v.clear(); // Nay } fn partial_drains() { let mut v = vec![1, 2, 3]; v.drain(1..); // Yay + let mut v = vec![1, 2, 3]; + v.drain(1..).max(); // Yay let mut v = vec![1, 2, 3]; v.drain(..v.len() - 1); // Yay + let mut v = vec![1, 2, 3]; + v.drain(..v.len() - 1).min(); // Yay let mut v = vec![1, 2, 3]; v.drain(1..v.len() - 1); // Yay + let mut v = vec![1, 2, 3]; + let w: Vec = v.drain(1..v.len() - 1).collect(); // Yay } fn main() {} diff --git a/tests/ui/clear_with_drain.rs b/tests/ui/clear_with_drain.rs index 721af7d2ea7..6926a4b60e8 100644 --- a/tests/ui/clear_with_drain.rs +++ b/tests/ui/clear_with_drain.rs @@ -3,38 +3,70 @@ #![warn(clippy::clear_with_drain)] fn range() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(0..u.len()); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(0..v.len()); // Yay + + let mut v = vec![1, 2, 3]; + let n = v.drain(0..v.len()).count(); // Yay + + let mut v = vec![1, 2, 3]; v.drain(0..v.len()); // Nay } fn range_from() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(0..); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(0..); // Yay + + let mut v = vec![1, 2, 3]; + let next = v.drain(0..).next(); // Yay + + let mut v = vec![1, 2, 3]; v.drain(0..); // Nay } fn range_full() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(..); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(..); // Yay + + let mut v = vec![1, 2, 3]; + // Yay + for x in v.drain(..) { + let y = format!("x = {x}"); + } + + let mut v = vec![1, 2, 3]; v.drain(..); // Nay } fn range_to() { - let (mut u, mut v) = (vec![1, 2, 3], vec![1, 2, 3]); - let iter = u.drain(..u.len()); // Yay + let mut v = vec![1, 2, 3]; + let iter = v.drain(..v.len()); // Yay + + let mut v = vec![1, 2, 3]; + // Yay + for x in v.drain(..v.len()) { + let y = format!("x = {x}"); + } + + let mut v = vec![1, 2, 3]; v.drain(..v.len()); // Nay } fn partial_drains() { let mut v = vec![1, 2, 3]; v.drain(1..); // Yay + let mut v = vec![1, 2, 3]; + v.drain(1..).max(); // Yay let mut v = vec![1, 2, 3]; v.drain(..v.len() - 1); // Yay + let mut v = vec![1, 2, 3]; + v.drain(..v.len() - 1).min(); // Yay let mut v = vec![1, 2, 3]; v.drain(1..v.len() - 1); // Yay + let mut v = vec![1, 2, 3]; + let w: Vec = v.drain(1..v.len() - 1).collect(); // Yay } fn main() {} diff --git a/tests/ui/clear_with_drain.stderr b/tests/ui/clear_with_drain.stderr index 0d6d8263e10..b7206cd9007 100644 --- a/tests/ui/clear_with_drain.stderr +++ b/tests/ui/clear_with_drain.stderr @@ -1,5 +1,5 @@ error: `drain` used to clear a `Vec` - --> $DIR/clear_with_drain.rs:8:7 + --> $DIR/clear_with_drain.rs:13:7 | LL | v.drain(0..v.len()); // Nay | ^^^^^^^^^^^^^^^^^ help: try: `clear()` @@ -7,19 +7,19 @@ LL | v.drain(0..v.len()); // Nay = note: `-D clippy::clear-with-drain` implied by `-D warnings` error: `drain` used to clear a `Vec` - --> $DIR/clear_with_drain.rs:14:7 + --> $DIR/clear_with_drain.rs:24:7 | LL | v.drain(0..); // Nay | ^^^^^^^^^^ help: try: `clear()` error: `drain` used to clear a `Vec` - --> $DIR/clear_with_drain.rs:20:7 + --> $DIR/clear_with_drain.rs:38:7 | LL | v.drain(..); // Nay | ^^^^^^^^^ help: try: `clear()` error: `drain` used to clear a `Vec` - --> $DIR/clear_with_drain.rs:26:7 + --> $DIR/clear_with_drain.rs:52:7 | LL | v.drain(..v.len()); // Nay | ^^^^^^^^^^^^^^^^ help: try: `clear()`