Skip to content

Commit 2f5d418

Browse files
committed
Add test case
1 parent 3ce820b commit 2f5d418

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/ui/needless_collect_indirect.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ fn main() {
1616
.into_iter()
1717
.map(|x| (*x, *x + 1))
1818
.collect::<HashMap<_, _>>();
19+
20+
// #6202
21+
let a = "a".to_string();
22+
let sample = vec![a.clone(), "b".to_string(), "c".to_string()];
23+
let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
24+
non_copy_contains.contains(&a);
1925
}

tests/ui/needless_collect_indirect.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,18 @@ LL |
5151
LL | sample.iter().any(|x| x == &5);
5252
|
5353

54-
error: aborting due to 4 previous errors
54+
error: avoid using `collect()` when not needed
55+
--> $DIR/needless_collect_indirect.rs:23:5
56+
|
57+
LL | / let non_copy_contains = sample.into_iter().collect::<Vec<_>>();
58+
LL | | non_copy_contains.contains(&a);
59+
| |____^
60+
|
61+
help: Check if the original Iterator contains an element instead of collecting then checking
62+
|
63+
LL |
64+
LL | sample.into_iter().any(|x| x == a);
65+
|
66+
67+
error: aborting due to 5 previous errors
5568

0 commit comments

Comments
 (0)