rust-lang / rust-lang/rust-clippy
`needless_collect` doesn't understand local borrows
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
When running clippy (0.0.212 (2020-09-17 f3c923a)) on https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=5b4bbd5f3b653c7d9d8a84d2a63903fa I get the suggestion:
Checking playground v0.0.1 (/playground)
warning: avoid using `collect()` when not needed
--> src/main.rs:4:5
|
4 | / let a_filt = a.into_iter().filter(|x| !b.contains(x)).collect::<Vec<_>>();
5 | |
6 | | for v in b.into_iter().chain(a_filt.into_iter()) {
| |_________________________________^
|
= note: `#[warn(clippy::needless_collect)]` on by default
help: Use the original Iterator instead of collecting it and then producing a new one
|
4 |
5 |
6 | for v in b.into_iter().chain(a.into_iter().filter(|x| !b.contains(x))) {
|
warning: 1 warning emitted
Finished dev [unoptimized + debuginfo] target(s) in 0.33s
However, the original Iterator cannot be used, as demonstrated by https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=65b782dbf6cd1731a6906afcd9704e99
Compiling playground v0.0.1 (/playground)
error[E0505]: cannot move out of `b` because it is borrowed
--> src/main.rs:6:14
|
4 | let a_filt = a.into_iter().filter(|x| !b.contains(x));
| --- - borrow occurs due to use in closure
| |
| borrow of `b` occurs here
5 |
6 | for v in b.into_iter().chain(a_filt) {
| ^ ------ borrow later used here
| |
| move out of `b` occurs here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0505`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the needless_collect warning with the linked Rust Playground examples, then inspect the lint's handling of local borrows. Confirm that the suggested iterator rewrite does not move a value while it is still borrowed, while valid needless-collect cases continue to be reported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100