Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive of needless_collect #6297

Closed
iddm opened this issue Nov 5, 2020 · 1 comment · Fixed by #6313
Closed

False-positive of needless_collect #6297

iddm opened this issue Nov 5, 2020 · 1 comment · Fixed by #6313
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@iddm
Copy link
Contributor

iddm commented Nov 5, 2020

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
@iddm iddm added the C-bug Category: Clippy is not doing the correct thing label Nov 5, 2020
@giraffate
Copy link
Contributor

related issue: #5991

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants