rust-lang / rust-lang/rust-clippy

False positive in `map_identity` with lifetimes

Open
#9,280 12 comments 3 reactions 1 assignee View on GitHub

@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

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.