Skip to content

False-positive of needless_collect #6297

Closed
@iddm

Description

@iddm

I tried this code:

fn main() {
    let items = vec![vec![1, 2],vec![3, 4], vec![5, 6]];
    let items2 = vec![2u64, 3u64];
    let filtered = items
            .iter()
            .map(|i| {
                i.iter().map(|j| j * 3).collect::<Vec<u64>>()
            })
            .flatten()
            .collect::<Vec<u64>>();

        if filtered.is_empty() {
            println!("1");
        } else {
            let found = items2.iter().filter(|i| filtered.iter().any(|s| s % **i == 0)).collect::<Vec<&u64>>();
            println!("{:?}", found);
        }
}

playground

I expected to see this happen: everything is fine as there is no other way to execute this code - you can't know in advance whether there are elements or not.

Instead, this happened: the lint says I can avoid collecting while I clearly can't.

Meta

  • cargo clippy -V: clippy 0.0.212 (18bf6b4 2020-10-07)
  • rustc -Vv:
rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions