mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Split indirect collects into their own test case
This commit is contained in:
parent
3657c92ac9
commit
c86f4109fd
6 changed files with 110 additions and 36 deletions
|
@ -18,15 +18,4 @@ fn main() {
|
|||
sample.iter().collect::<HashSet<_>>().len();
|
||||
// Neither should this
|
||||
sample.iter().collect::<BTreeSet<_>>().len();
|
||||
let indirect_positive = sample.iter().collect::<Vec<_>>();
|
||||
indirect_positive
|
||||
.into_iter()
|
||||
.map(|x| (x, x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let indirect_negative = sample.iter().collect::<Vec<_>>();
|
||||
indirect_negative.len();
|
||||
indirect_negative
|
||||
.iter()
|
||||
.map(|x| (*x, *x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
}
|
||||
|
|
|
@ -18,15 +18,4 @@ fn main() {
|
|||
sample.iter().collect::<HashSet<_>>().len();
|
||||
// Neither should this
|
||||
sample.iter().collect::<BTreeSet<_>>().len();
|
||||
let indirect_positive = sample.iter().collect::<Vec<_>>();
|
||||
indirect_positive
|
||||
.into_iter()
|
||||
.map(|x| (x, x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let indirect_negative = sample.iter().collect::<Vec<_>>();
|
||||
indirect_negative.len();
|
||||
indirect_negative
|
||||
.iter()
|
||||
.map(|x| (*x, *x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
}
|
||||
|
|
|
@ -1,21 +1,10 @@
|
|||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect.rs:21:5
|
||||
|
|
||||
LL | let indirect_positive = sample.iter().collect::<Vec<_>>();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
||||
help: Use the original Iterator instead of collecting it and then producing a new one
|
||||
|
|
||||
LL |
|
||||
LL | sample.iter()
|
||||
|
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect.rs:11:29
|
||||
|
|
||||
LL | let len = sample.iter().collect::<Vec<_>>().len();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
|
||||
|
|
||||
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect.rs:12:15
|
||||
|
@ -35,5 +24,5 @@ error: avoid using `collect()` when not needed
|
|||
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
26
tests/ui/needless_collect_indirect.fixed
Normal file
26
tests/ui/needless_collect_indirect.fixed
Normal file
|
@ -0,0 +1,26 @@
|
|||
// run-rustfix
|
||||
|
||||
#[allow(unused)]
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
fn main() {
|
||||
let sample = [1; 5];
|
||||
let indirect_iter = sample.iter().collect::<Vec<_>>();
|
||||
indirect_iter
|
||||
.into_iter()
|
||||
.map(|x| (x, x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let indirect_len = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_len.len();
|
||||
let indirect_empty = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_empty.is_empty();
|
||||
let indirect_contains = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_contains.contains(&&5);
|
||||
let indirect_negative = sample.iter().collect::<Vec<_>>();
|
||||
indirect_negative.len();
|
||||
indirect_negative
|
||||
.into_iter()
|
||||
.map(|x| (*x, *x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
}
|
26
tests/ui/needless_collect_indirect.rs
Normal file
26
tests/ui/needless_collect_indirect.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
// run-rustfix
|
||||
|
||||
#[allow(unused)]
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
fn main() {
|
||||
let sample = [1; 5];
|
||||
let indirect_iter = sample.iter().collect::<Vec<_>>();
|
||||
indirect_iter
|
||||
.into_iter()
|
||||
.map(|x| (x, x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let indirect_len = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_len.len();
|
||||
let indirect_empty = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_empty.is_empty();
|
||||
let indirect_contains = sample.iter().collect::<VecDeque<_>>();
|
||||
indirect_contains.contains(&&5);
|
||||
let indirect_negative = sample.iter().collect::<Vec<_>>();
|
||||
indirect_negative.len();
|
||||
indirect_negative
|
||||
.into_iter()
|
||||
.map(|x| (*x, *x + 1))
|
||||
.collect::<HashMap<_, _>>();
|
||||
}
|
55
tests/ui/needless_collect_indirect.stderr
Normal file
55
tests/ui/needless_collect_indirect.stderr
Normal file
|
@ -0,0 +1,55 @@
|
|||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect_indirect.rs:9:5
|
||||
|
|
||||
LL | / let indirect_iter = sample.iter().collect::<Vec<_>>();
|
||||
LL | | indirect_iter
|
||||
| |____^
|
||||
|
|
||||
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
||||
help: Use the original Iterator instead of collecting it and then producing a new one
|
||||
|
|
||||
LL |
|
||||
LL | sample.iter()
|
||||
|
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect_indirect.rs:14:5
|
||||
|
|
||||
LL | / let indirect_len = sample.iter().collect::<VecDeque<_>>();
|
||||
LL | | indirect_len.len();
|
||||
| |____^
|
||||
|
|
||||
help: Take the original Iterator's count instead of collecting it and finding the length
|
||||
|
|
||||
LL |
|
||||
LL | sample.iter().count();
|
||||
|
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect_indirect.rs:16:5
|
||||
|
|
||||
LL | / let indirect_empty = sample.iter().collect::<VecDeque<_>>();
|
||||
LL | | indirect_empty.is_empty();
|
||||
| |____^
|
||||
|
|
||||
help: Check if the original Iterator has anything instead of collecting it and seeing if it's empty
|
||||
|
|
||||
LL |
|
||||
LL | sample.iter().next().is_none();
|
||||
|
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> $DIR/needless_collect_indirect.rs:18:5
|
||||
|
|
||||
LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>();
|
||||
LL | | indirect_contains.contains(&&5);
|
||||
| |____^
|
||||
|
|
||||
help: Check if the original Iterator contains an element instead of collecting then checking
|
||||
|
|
||||
LL |
|
||||
LL | sample.iter().any(|x| x == &&5);
|
||||
|
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
Loading…
Reference in a new issue