]> git.lizzy.rs Git - rust.git/commitdiff
Add test case
authorTakayuki Nakata <f.seasons017@gmail.com>
Sun, 25 Oct 2020 14:55:41 +0000 (23:55 +0900)
committerTakayuki Nakata <f.seasons017@gmail.com>
Sun, 25 Oct 2020 15:01:20 +0000 (00:01 +0900)
tests/ui/needless_collect_indirect.rs
tests/ui/needless_collect_indirect.stderr

index 4cf03e820352356b84f873cac15c7fd094a8020b..4f6e53577273cf1b05cc33ca9aaed6a3260abf62 100644 (file)
@@ -16,4 +16,10 @@ fn main() {
         .into_iter()
         .map(|x| (*x, *x + 1))
         .collect::<HashMap<_, _>>();
+
+    // #6202
+    let a = "a".to_string();
+    let sample = vec![a.clone(), "b".to_string(), "c".to_string()];
+    let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
+    non_copy_contains.contains(&a);
 }
index 7b8e227f304c1b4a64ed4f77f7fe3ce7f4ee2a0e..fb807da5f8abee5b3339dcfffe92eb6e16532264 100644 (file)
@@ -51,5 +51,18 @@ LL |
 LL |     sample.iter().any(|x| x == &5);
    |
 
-error: aborting due to 4 previous errors
+error: avoid using `collect()` when not needed
+  --> $DIR/needless_collect_indirect.rs:23:5
+   |
+LL | /     let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
+LL | |     non_copy_contains.contains(&a);
+   | |____^
+   |
+help: Check if the original Iterator contains an element instead of collecting then checking
+   |
+LL |     
+LL |     sample.into_iter().any(|x| x == a);
+   |
+
+error: aborting due to 5 previous errors