Skip to content

manual_flatten suggests uncompilable code on slice of references #6893

@Cxarli

Description

@Cxarli

Lint name: manual_flatten

For the following code:

let x = &[ &Some(1) ];
    
// warning: unnecessary `if let` since only the `Some` variant of the iterator element is used
for n in x {
    if let Some(n) = n {
        println!("{:?}", n);
    }
}

The lint suggests replacing the loop with:

for n in x.into_iter().flatten() {
    println!("{:?}", n);
}

However, this doesn't compile, because &&Option<{integer}> is not an iterator.

What does compile is

for n in x.into_iter().copied().flatten() {
    println!("{:?}", n);
}

Meta

$ cargo clippy -V
clippy 0.1.52 (a15f484 2021-02-22)

$ rustc -Vv
rustc 1.52.0-nightly (a15f484b9 2021-02-22)
binary: rustc
commit-hash: a15f484b918a4533ad633ea903ccce82910af342
commit-date: 2021-02-22
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions