rust-lang / rust-lang/rust-clippy

`needless_collect` false positive

Open
#7,512 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive L-nursery
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: needless_collect

Reduced code:

pub struct Foo {
    foo: bool,
    bar: bool,
}

pub fn test(foo: Vec<Foo>) {
    let bar: Vec<_> = foo.iter().map(|f|f.bar).collect();
    drop(foo);

    for (_, _) in bar.into_iter().enumerate() {

    }    
}

// pub fn incorrect_suggestion(foo: Vec<Foo>) {
//     
//     drop(foo);
// 
//     for (_, _) in foo.iter().map(|f|f.bar).enumerate() {
// 
//     }    
// }

I expected no warning here because foo is dropped. The incorrect suggestion also appears if foo is shadowed, potentially generating incorrect code that compiles.

Meta
  • cargo clippy -V: clippy 0.1.54 (a178d032 2021-07-26)
  • rustc -Vv:
rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-pc-windows-msvc
release: 1.54.0
LLVM version: 12.0.1

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

Start with the needless_collect lint implementation and its existing tests, then run the reduced Rust example to reproduce the false positive and incorrect suggestion. The fix is complete when an explicit drop(foo) or shadowed foo no longer produces this warning or suggestion, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.