Open
Description
Description
lint: https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_identity
This was originally added as being like flat_map_identity
(#6685), but they're not quite analogous. .flatten()
is unambiguously better than .flat_map(identity)
, but the same isn't true for .filter_map(identity)
.
The problem is that (as I recently mentioned in rust-lang/rust#99230 (comment)), FlatMap
(including Flatten
) is a fundamentally harder problem than FilterMap
, because it needs to deal in iterators that might have more than one item. And thus, for example,
let flatten_it = a.iter().copied().flatten();
let filter_map_it = a.iter().copied().filter_map(|x| x);
dbg!([size_of_val(&flatten_it), size_of_val(&filter_map_it)]); // 32, 16
dbg!([flatten_it.size_hint(), filter_map_it.size_hint()]); // (0, None), (0, Some(3))
So perhaps this shouldn't be warn-by-default when it would also be plausible to have a performance
-category lint to say to do exactly the opposite.
Version
(Not really version-specific; inherent to the definition of the lint.)
Additional Labels
@rustbot label +I-false-positive