Skip to content

False positive for unnecessary_filter_map #6804

@meiamsome

Description

@meiamsome

Lint name: unnecessary_filter_map

I tried this code:

fn test() -> impl Iterator<Item = u64> {
    vec![()].into_iter().filter_map(|_| None)
}

I expected to see this happen:
The filter map is required because the Item is being changed from () to u64, so I expected this to not be linted.

Instead, this happened:
The code is linted as failing unnecessary_filter_map, despite the use of filter_map being required:

warning: this `.filter_map` can be written more simply using `.filter`

But filter does not compile because that does not do the necessary type conversion for Item:

fn test() -> impl Iterator<Item = u64> {
    vec![()].into_iter().filter(|_| false)
}
error[E0271]: type mismatch resolving `<std::iter::Filter<std::vec::IntoIter<()>, [closure@<location>]> as Iterator>::Item == u64`
 --> <location>
  |
7 | fn test() -> impl Iterator<Item = u64> {
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `u64`

Meta

  • cargo clippy -V: clippy 0.0.212 (cb75ad5 2021-02-10)
  • rustc -Vv:
    rustc 1.50.0 (cb75ad5db 2021-02-10)
    binary: rustc
    commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
    commit-date: 2021-02-10
    host: x86_64-unknown-linux-gnu
    release: 1.50.0
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions