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 for unnecessary_filter_map #6804

Closed
meiamsome opened this issue Feb 26, 2021 · 1 comment · Fixed by #7175
Closed

False positive for unnecessary_filter_map #6804

meiamsome opened this issue Feb 26, 2021 · 1 comment · Fixed by #7175
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@meiamsome
Copy link

meiamsome commented Feb 26, 2021

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
    
@meiamsome meiamsome added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 26, 2021
@camsteffen
Copy link
Contributor

Interesting. Do you have a more practical example?

@camsteffen camsteffen added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label Feb 26, 2021
@bors bors closed this as completed in 9dd8705 May 5, 2021
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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants