Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nihohit opened this issue Dec 29, 2023 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@nihohit
Copy link

nihohit commented Dec 29, 2023

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

@nihohit nihohit added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 29, 2023
@dannycjones
Copy link

Looks similar to #11764 which has a fix but I'm unsure when that'll be released into stable.

@PartiallyUntyped
Copy link
Contributor

PartiallyUntyped commented Dec 29, 2023

@dannycjones is correct, this has been fixed with #11792

Running uibless on latest commit:

TESTNAME=map_identity cargo uibless && git diff

--- a/tests/ui/map_identity.fixed
+++ b/tests/ui/map_identity.fixed
@@ -61,3 +61,12 @@ fn issue11764() {
     // no match ergonomics for `(i32, i32)`
     let _ = x.iter().copied();
 }
+
+fn issue12043() {
+    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();
+}
+
diff --git a/tests/ui/map_identity.rs b/tests/ui/map_identity.rs
index c646c0568..e0f941e6c 100644
--- a/tests/ui/map_identity.rs
+++ b/tests/ui/map_identity.rs
@@ -65,3 +65,12 @@ fn issue11764() {
     // no match ergonomics for `(i32, i32)`
     let _ = x.iter().copied().map(|(x, y)| (x, y));
 }
+
+fn issue12043() {
+    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();
+}
+

@blyxyas
Copy link
Member

blyxyas commented Dec 29, 2023

I'll close this then ~(=^‥^)/

@blyxyas blyxyas closed this as completed Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

4 participants