rust-lang / rust-lang/rust-clippy

`filter_map_identity` is arguably overzealous

Open
#9,377 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-false-positive
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))

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f1ccc42f95540d435e6d7caa58d29ed8

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.