rust-lang / rust-lang/rust-clippy

`needless_collect` doesn't understand local borrows

Open
#6,066 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug E-medium L-nursery
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.