about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2023-10-29 05:11:52 +0100
committerNadrieril <nadrieril+git@gmail.com>2023-11-02 03:19:19 +0100
commitb60f08a66d5a545d414a70a7047161cb191054b9 (patch)
tree2ceaa0832e2a692576cca1f66389c9784ae965e4
parentd95f6a9532708e13d779e431384daa64b638703e (diff)
downloadrust-b60f08a66d5a545d414a70a7047161cb191054b9.tar.gz
rust-b60f08a66d5a545d414a70a7047161cb191054b9.zip
Add regression test for pattern checks
-rw-r--r--tests/ui/pattern/usefulness/conflicting_bindings.rs22
-rw-r--r--tests/ui/pattern/usefulness/conflicting_bindings.stderr50
2 files changed, 72 insertions, 0 deletions
diff --git a/tests/ui/pattern/usefulness/conflicting_bindings.rs b/tests/ui/pattern/usefulness/conflicting_bindings.rs
new file mode 100644
index 00000000000..1df4425c48b
--- /dev/null
+++ b/tests/ui/pattern/usefulness/conflicting_bindings.rs
@@ -0,0 +1,22 @@
+#![feature(if_let_guard, let_chains)]
+
+fn main() {
+    let mut x = Some(String::new());
+    let ref mut y @ ref mut z = x;
+    //~^ ERROR: mutable more than once
+    let Some(ref mut y @ ref mut z) = x else { return };
+    //~^ ERROR: mutable more than once
+    if let Some(ref mut y @ ref mut z) = x {}
+    //~^ ERROR: mutable more than once
+    if let Some(ref mut y @ ref mut z) = x && true {}
+    while let Some(ref mut y @ ref mut z) = x {}
+    //~^ ERROR: mutable more than once
+    while let Some(ref mut y @ ref mut z) = x && true {}
+    match x {
+        ref mut y @ ref mut z => {} //~ ERROR: mutable more than once
+    }
+    match () {
+        () if let Some(ref mut y @ ref mut z) = x => {} //~ ERROR: mutable more than once
+        _ => {}
+    }
+}
diff --git a/tests/ui/pattern/usefulness/conflicting_bindings.stderr b/tests/ui/pattern/usefulness/conflicting_bindings.stderr
new file mode 100644
index 00000000000..af20eece20c
--- /dev/null
+++ b/tests/ui/pattern/usefulness/conflicting_bindings.stderr
@@ -0,0 +1,50 @@
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:5:9
+   |
+LL |     let ref mut y @ ref mut z = x;
+   |         ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |         |
+   |         value is mutably borrowed by `y` here
+
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:7:14
+   |
+LL |     let Some(ref mut y @ ref mut z) = x else { return };
+   |              ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |              |
+   |              value is mutably borrowed by `y` here
+
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:9:17
+   |
+LL |     if let Some(ref mut y @ ref mut z) = x {}
+   |                 ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |                 |
+   |                 value is mutably borrowed by `y` here
+
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:12:20
+   |
+LL |     while let Some(ref mut y @ ref mut z) = x {}
+   |                    ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |                    |
+   |                    value is mutably borrowed by `y` here
+
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:16:9
+   |
+LL |         ref mut y @ ref mut z => {}
+   |         ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |         |
+   |         value is mutably borrowed by `y` here
+
+error: cannot borrow value as mutable more than once at a time
+  --> $DIR/conflicting_bindings.rs:19:24
+   |
+LL |         () if let Some(ref mut y @ ref mut z) = x => {}
+   |                        ^^^^^^^^^   --------- value is mutably borrowed by `z` here
+   |                        |
+   |                        value is mutably borrowed by `y` here
+
+error: aborting due to 6 previous errors
+