rust-lang / rust-lang/rust-clippy
False positive in `map_identity` with lifetimes
Open
@cyqsimon is already working on this.
Since Nov 5, 2023.
C-bug
I-false-positive
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Mapping with an identity closure (|x| x) is sometimes required to fix lifetimes.
Lint Name
map_identity
Reproducer
I tried this code:
pub fn test<'a, I>(iter: I)
where
I: IntoIterator<Item = &'a i32>
{
let local = 0;
let _ = std::iter::once(&local).chain(iter.into_iter().map(|x| x));
}
Removing the map results in a compile error.
I saw this happen:
warning: unnecessary map of the identity function
--> src/lib.rs:6:59
|
6 | let _ = std::iter::once(&local).chain(iter.into_iter().map(|x| x));
| ^^^^^^^^^^^ help: remove the call to `map`
|
= note: `#[warn(clippy::map_identity)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity
I expected to see this happen:
No warning, as the map cannot be removed without causing a compile error. This happens on both stable and nightly.
Version
rustc 1.64.0-nightly (0f4bcadb4 2022-07-30)
binary: rustc
commit-hash: 0f4bcadb46006bc484dad85616b484f93879ca4e
commit-date: 2022-07-30
host: x86_64-pc-windows-msvc
release: 1.64.0-nightly
LLVM version: 14.0.6
Additional Labels
@rustbot label +I-suggestion-causes-error
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.
Assessment
This issue has not been assessed yet.