rust-lang / rust-lang/rust-clippy
`filter_map_identity` is arguably overzealous
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
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 (https://github.com/rust-lang/rust-clippy/pull/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 https://github.com/rust-lang/rust/pull/99230#discussion_r955364588), 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the filter_map_identity lint documentation linked in the issue and compare its rationale with the supplied size and size_hint examples. No source file or test is named, so locate the lint implementation and existing tests before deciding whether the default warning behavior is appropriate. Done means the lint's behavior and rationale clearly reflect the decision.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100