Skip to content

Clippy reports map with identity function on functions that perform implicit conversion #12043

Closed
@nihohit

Description

@nihohit

Summary

Functions that seem like identity functions, but contain implicit conversions such as &(A,B) -> (&A, &B) are warned against, even if they're required for compilation.

Example - line 7 cannot be removed, but clippy warns against it.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c27e3a77ebb1c965883045a67a8375b3

Lint Name

map_identity

Reproducer

I tried this code:

fn main() {
    let vec = [(1,2),(3,4)];
    let (k,v):(Vec<&usize>, Vec<&usize>) = vec
        .iter()
        .map(|(k,v)|(k,v)) // this isn't the identity function - it's a conversion &(K,V) -> (&K, &V)
        .unzip();
    println!("{k:?}, {v:?}");
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: unnecessary map of the identity function
 --> src/main.rs:6:16
  |
6 |           .iter()
  |  ________________^
7 | |         .map(|(k,v)|(k,v)) // this isn't the identity function - it's a conversion &(K,V) -> (&K, &V)
  | |__________________________^ help: remove the call to `map`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity
  = note: `#[warn(clippy::map_identity)]` on by default

I expected to see this happen:
no lint.

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions