Skip to content

Commit c86f410

Browse files
committed
Split indirect collects into their own test case
1 parent 3657c92 commit c86f410

6 files changed

+110
-36
lines changed

tests/ui/needless_collect.fixed

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,4 @@ fn main() {
1818
sample.iter().collect::<HashSet<_>>().len();
1919
// Neither should this
2020
sample.iter().collect::<BTreeSet<_>>().len();
21-
let indirect_positive = sample.iter().collect::<Vec<_>>();
22-
indirect_positive
23-
.into_iter()
24-
.map(|x| (x, x + 1))
25-
.collect::<HashMap<_, _>>();
26-
let indirect_negative = sample.iter().collect::<Vec<_>>();
27-
indirect_negative.len();
28-
indirect_negative
29-
.iter()
30-
.map(|x| (*x, *x + 1))
31-
.collect::<HashMap<_, _>>();
3221
}

tests/ui/needless_collect.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,4 @@ fn main() {
1818
sample.iter().collect::<HashSet<_>>().len();
1919
// Neither should this
2020
sample.iter().collect::<BTreeSet<_>>().len();
21-
let indirect_positive = sample.iter().collect::<Vec<_>>();
22-
indirect_positive
23-
.into_iter()
24-
.map(|x| (x, x + 1))
25-
.collect::<HashMap<_, _>>();
26-
let indirect_negative = sample.iter().collect::<Vec<_>>();
27-
indirect_negative.len();
28-
indirect_negative
29-
.iter()
30-
.map(|x| (*x, *x + 1))
31-
.collect::<HashMap<_, _>>();
3221
}

tests/ui/needless_collect.stderr

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
error: avoid using `collect()` when not needed
2-
--> $DIR/needless_collect.rs:21:5
3-
|
4-
LL | let indirect_positive = sample.iter().collect::<Vec<_>>();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D clippy::needless-collect` implied by `-D warnings`
8-
help: Use the original Iterator instead of collecting it and then producing a new one
9-
|
10-
LL |
11-
LL | sample.iter()
12-
|
13-
141
error: avoid using `collect()` when not needed
152
--> $DIR/needless_collect.rs:11:29
163
|
174
LL | let len = sample.iter().collect::<Vec<_>>().len();
185
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
6+
|
7+
= note: `-D clippy::needless-collect` implied by `-D warnings`
198

209
error: avoid using `collect()` when not needed
2110
--> $DIR/needless_collect.rs:12:15
@@ -35,5 +24,5 @@ error: avoid using `collect()` when not needed
3524
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
3625
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
3726

38-
error: aborting due to 5 previous errors
27+
error: aborting due to 4 previous errors
3928

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run-rustfix
2+
3+
#[allow(unused)]
4+
5+
use std::collections::{HashMap, VecDeque};
6+
7+
fn main() {
8+
let sample = [1; 5];
9+
let indirect_iter = sample.iter().collect::<Vec<_>>();
10+
indirect_iter
11+
.into_iter()
12+
.map(|x| (x, x + 1))
13+
.collect::<HashMap<_, _>>();
14+
let indirect_len = sample.iter().collect::<VecDeque<_>>();
15+
indirect_len.len();
16+
let indirect_empty = sample.iter().collect::<VecDeque<_>>();
17+
indirect_empty.is_empty();
18+
let indirect_contains = sample.iter().collect::<VecDeque<_>>();
19+
indirect_contains.contains(&&5);
20+
let indirect_negative = sample.iter().collect::<Vec<_>>();
21+
indirect_negative.len();
22+
indirect_negative
23+
.into_iter()
24+
.map(|x| (*x, *x + 1))
25+
.collect::<HashMap<_, _>>();
26+
}

tests/ui/needless_collect_indirect.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run-rustfix
2+
3+
#[allow(unused)]
4+
5+
use std::collections::{HashMap, VecDeque};
6+
7+
fn main() {
8+
let sample = [1; 5];
9+
let indirect_iter = sample.iter().collect::<Vec<_>>();
10+
indirect_iter
11+
.into_iter()
12+
.map(|x| (x, x + 1))
13+
.collect::<HashMap<_, _>>();
14+
let indirect_len = sample.iter().collect::<VecDeque<_>>();
15+
indirect_len.len();
16+
let indirect_empty = sample.iter().collect::<VecDeque<_>>();
17+
indirect_empty.is_empty();
18+
let indirect_contains = sample.iter().collect::<VecDeque<_>>();
19+
indirect_contains.contains(&&5);
20+
let indirect_negative = sample.iter().collect::<Vec<_>>();
21+
indirect_negative.len();
22+
indirect_negative
23+
.into_iter()
24+
.map(|x| (*x, *x + 1))
25+
.collect::<HashMap<_, _>>();
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error: avoid using `collect()` when not needed
2+
--> $DIR/needless_collect_indirect.rs:9:5
3+
|
4+
LL | / let indirect_iter = sample.iter().collect::<Vec<_>>();
5+
LL | | indirect_iter
6+
| |____^
7+
|
8+
= note: `-D clippy::needless-collect` implied by `-D warnings`
9+
help: Use the original Iterator instead of collecting it and then producing a new one
10+
|
11+
LL |
12+
LL | sample.iter()
13+
|
14+
15+
error: avoid using `collect()` when not needed
16+
--> $DIR/needless_collect_indirect.rs:14:5
17+
|
18+
LL | / let indirect_len = sample.iter().collect::<VecDeque<_>>();
19+
LL | | indirect_len.len();
20+
| |____^
21+
|
22+
help: Take the original Iterator's count instead of collecting it and finding the length
23+
|
24+
LL |
25+
LL | sample.iter().count();
26+
|
27+
28+
error: avoid using `collect()` when not needed
29+
--> $DIR/needless_collect_indirect.rs:16:5
30+
|
31+
LL | / let indirect_empty = sample.iter().collect::<VecDeque<_>>();
32+
LL | | indirect_empty.is_empty();
33+
| |____^
34+
|
35+
help: Check if the original Iterator has anything instead of collecting it and seeing if it's empty
36+
|
37+
LL |
38+
LL | sample.iter().next().is_none();
39+
|
40+
41+
error: avoid using `collect()` when not needed
42+
--> $DIR/needless_collect_indirect.rs:18:5
43+
|
44+
LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>();
45+
LL | | indirect_contains.contains(&&5);
46+
| |____^
47+
|
48+
help: Check if the original Iterator contains an element instead of collecting then checking
49+
|
50+
LL |
51+
LL | sample.iter().any(|x| x == &&5);
52+
|
53+
54+
error: aborting due to 4 previous errors
55+

0 commit comments

Comments
 (0)