-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #5837 - JarredAllen:needless_collect, r=phansch
needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ... changelog: Expand the needless_collect lint as suggested in #5627 (WIP). This PR is WIP because I can't figure out how to make the multi-part suggestion include its changes in the source code (the fixed is identical to the source, despite the lint making suggestions). Aside from that one issue, I think this should be good.
- Loading branch information
Showing
3 changed files
with
235 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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<_, _>>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
error: avoid using `collect()` when not needed | ||
--> $DIR/needless_collect_indirect.rs:5:5 | ||
| | ||
LL | / let indirect_iter = sample.iter().collect::<Vec<_>>(); | ||
LL | | indirect_iter.into_iter().map(|x| (x, x + 1)).collect::<HashMap<_, _>>(); | ||
| |____^ | ||
| | ||
= 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().map(|x| (x, x + 1)).collect::<HashMap<_, _>>(); | ||
| | ||
|
||
error: avoid using `collect()` when not needed | ||
--> $DIR/needless_collect_indirect.rs:7: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:9: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:11: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 | ||
|