about summary refs log tree commit diff
path: root/tests/ui
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2025-01-07 13:43:46 +0000
committerGitHub <noreply@github.com>2025-01-07 13:43:46 +0000
commitf5ca68f9db105f52aa1bf2e4eeb798fc86a10f6b (patch)
treeffd65c357a0b0965bfdb6bc281a1cbbcda4ae572 /tests/ui
parentb7b69b135447dd2d4e3747880fd9998e6514f70f (diff)
parenta9fe04335a10bdcc5f9921be064e3f4f5f49e31d (diff)
downloadrust-f5ca68f9db105f52aa1bf2e4eeb798fc86a10f6b.tar.gz
rust-f5ca68f9db105f52aa1bf2e4eeb798fc86a10f6b.zip
Do not remove identity mapping if mandatory mutability would be lost (#13905)
Removing `.map(identity)` may result in invalid code if the receiver of
`map()` is an immutable binding, and the result of `map()` is used as
the receiver of a method call expecting a mutable reference.

Fix #13904

changelog: [`map_identity`]: do not lint if this would cause mandatory
mutability to be lost
Diffstat (limited to 'tests/ui')
-rw-r--r--tests/ui/map_identity.fixed15
-rw-r--r--tests/ui/map_identity.rs15
-rw-r--r--tests/ui/map_identity.stderr14
3 files changed, 43 insertions, 1 deletions
diff --git a/tests/ui/map_identity.fixed b/tests/ui/map_identity.fixed
index 53ebfb40ba0..3257ddc6f72 100644
--- a/tests/ui/map_identity.fixed
+++ b/tests/ui/map_identity.fixed
@@ -61,3 +61,18 @@ fn issue11764() {
     // no match ergonomics for `(i32, i32)`
     let _ = x.iter().copied();
 }
+
+fn issue13904() {
+    // don't lint: `it.next()` would not be legal as `it` is immutable
+    let it = [1, 2, 3].into_iter();
+    let _ = it.map(|x| x).next();
+
+    // lint
+    #[allow(unused_mut)]
+    let mut it = [1, 2, 3].into_iter();
+    let _ = it.next();
+
+    // lint
+    let it = [1, 2, 3].into_iter();
+    let _ = { it }.next();
+}
diff --git a/tests/ui/map_identity.rs b/tests/ui/map_identity.rs
index c646c056859..be3bb9a4f10 100644
--- a/tests/ui/map_identity.rs
+++ b/tests/ui/map_identity.rs
@@ -65,3 +65,18 @@ fn issue11764() {
     // no match ergonomics for `(i32, i32)`
     let _ = x.iter().copied().map(|(x, y)| (x, y));
 }
+
+fn issue13904() {
+    // don't lint: `it.next()` would not be legal as `it` is immutable
+    let it = [1, 2, 3].into_iter();
+    let _ = it.map(|x| x).next();
+
+    // lint
+    #[allow(unused_mut)]
+    let mut it = [1, 2, 3].into_iter();
+    let _ = it.map(|x| x).next();
+
+    // lint
+    let it = [1, 2, 3].into_iter();
+    let _ = { it }.map(|x| x).next();
+}
diff --git a/tests/ui/map_identity.stderr b/tests/ui/map_identity.stderr
index 0a0dc9c8f07..aa3fc4ae0b5 100644
--- a/tests/ui/map_identity.stderr
+++ b/tests/ui/map_identity.stderr
@@ -73,5 +73,17 @@ error: unnecessary map of the identity function
 LL |     let _ = x.iter().copied().map(|(x, y)| (x, y));
    |                              ^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
 
-error: aborting due to 11 previous errors
+error: unnecessary map of the identity function
+  --> tests/ui/map_identity.rs:77:15
+   |
+LL |     let _ = it.map(|x| x).next();
+   |               ^^^^^^^^^^^ help: remove the call to `map`
+
+error: unnecessary map of the identity function
+  --> tests/ui/map_identity.rs:81:19
+   |
+LL |     let _ = { it }.map(|x| x).next();
+   |                   ^^^^^^^^^^^ help: remove the call to `map`
+
+error: aborting due to 13 previous errors