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

filter_map suggestion on flat_map is wrong #7050

Closed
ghost opened this issue Apr 8, 2021 · 0 comments · Fixed by #7059
Closed

filter_map suggestion on flat_map is wrong #7050

ghost opened this issue Apr 8, 2021 · 0 comments · Fixed by #7059
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@ghost
Copy link

ghost commented Apr 8, 2021

Lint name: filter_map

I tried this code:

#![warn(clippy::filter_map)]

fn main() {
    let x = ["a", "abc", "abdc", "efg"];
    let c: String = x.iter()
        .filter(|s| s.starts_with('a'))
        .flat_map(|f| f.chars())
        .collect();       
   
    assert_eq!(c, "aabcabdc");
}

I expected to see this happen: no warning

Instead, this happened:

Checking playground v0.0.1 (/playground)
warning: called `filter(..).flat_map(..)` on an `Iterator`
 --> src/main.rs:5:21
  |
5 |       let c: String = x.iter()
  |  _____________________^
6 | |         .filter(|s| s.starts_with('a'))
7 | |         .flat_map(|f| f.chars())
  | |________________________________^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::filter_map)]
  |         ^^^^^^^^^^^^^^^^^^
  = help: this is more succinctly expressed by calling `.flat_map(..)` and filtering by returning `iter::empty()`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#filter_map

warning: 1 warning emitted

This warning is special case of filter_map for flat_map that should be completely removed.

This suggestion is incorrect and can never work. The flat_map function must return a single type of iterator. It can't conditionally return iter::empty(). That won't compile.

Compiling playground v0.0.1 (/playground)
error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:17:17
   |
14 | /             if s.starts_with('a') {
15 | |                 s.chars()
   | |                 --------- expected because of this
16 | |             } else {
17 | |                 std::iter::empty()
   | |                 ^^^^^^^^^^^^^^^^^^ expected struct `Chars`, found struct `std::iter::Empty`
18 | |             }
   | |_____________- `if` and `else` have incompatible types
   |
   = note: expected type `Chars<'_>`
            found struct `std::iter::Empty<_>`

Playground

@ghost ghost 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 Apr 8, 2021
@camsteffen camsteffen self-assigned this Apr 9, 2021
@bors bors closed this as completed in a6b514c Apr 11, 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 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.

1 participant