about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorYaron Tausky <yaron.tausky@gmail.com>2018-05-25 20:33:15 +0200
committerYaron Tausky <yaron.tausky@gmail.com>2018-06-01 23:17:10 +0200
commit3303e6847b4e8b413ec65a166337e5f9f8325f28 (patch)
tree4ec17594e97b989334beda8e1039a7af1c9365b4 /src/test
parentaa094a43cc041c8483b7c80fb0ec4be233dd01b7 (diff)
downloadrust-3303e6847b4e8b413ec65a166337e5f9f8325f28.tar.gz
rust-3303e6847b4e8b413ec65a166337e5f9f8325f28.zip
Suggest not mutably borrowing a mutable reference
This commit is concerned with the case where the user tries to mutably
borrow a mutable reference, thereby triggering an error. Instead of the
existing suggestion to make the binding mutable, the compiler will now
suggest to avoid borrowing altogether.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr9
-rw-r--r--src/test/ui/borrowck/mut-borrow-of-mut-ref.rs21
-rw-r--r--src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr13
-rw-r--r--src/test/ui/did_you_mean/issue-31424.stderr6
4 files changed, 47 insertions, 2 deletions
diff --git a/src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr b/src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr
new file mode 100644
index 00000000000..f8b84bce04e
--- /dev/null
+++ b/src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr
@@ -0,0 +1,9 @@
+error[E0596]: cannot borrow immutable item `b` as mutable
+  --> $DIR/mut-borrow-of-mut-ref.rs:18:7
+   |
+LL |     g(&mut b) //~ ERROR cannot borrow
+   |       ^^^^^^ cannot borrow as mutable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0596`.
diff --git a/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs b/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs
new file mode 100644
index 00000000000..75b9da52023
--- /dev/null
+++ b/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs
@@ -0,0 +1,21 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Suggest not mutably borrowing a mutable reference
+
+fn main() {
+    f(&mut 0)
+}
+
+fn f(b: &mut i32) {
+    g(&mut b) //~ ERROR cannot borrow
+}
+
+fn g(_: &mut i32) {}
diff --git a/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr b/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr
new file mode 100644
index 00000000000..885164cdc06
--- /dev/null
+++ b/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr
@@ -0,0 +1,13 @@
+error[E0596]: cannot borrow immutable argument `b` as mutable
+  --> $DIR/mut-borrow-of-mut-ref.rs:18:12
+   |
+LL |     g(&mut b) //~ ERROR cannot borrow
+   |            ^ cannot borrow mutably
+help: consider removing the `&mut`, as it is an immutable binding to a mutable reference
+   |
+LL |     g(b) //~ ERROR cannot borrow
+   |       ^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0596`.
diff --git a/src/test/ui/did_you_mean/issue-31424.stderr b/src/test/ui/did_you_mean/issue-31424.stderr
index a80593e05f1..9d0ab21ffaf 100644
--- a/src/test/ui/did_you_mean/issue-31424.stderr
+++ b/src/test/ui/did_you_mean/issue-31424.stderr
@@ -10,10 +10,12 @@ LL |         (&mut self).bar(); //~ ERROR cannot borrow
 error[E0596]: cannot borrow immutable argument `self` as mutable
   --> $DIR/issue-31424.rs:23:15
    |
-LL |     fn bar(self: &mut Self) {
-   |            --------------- consider changing this to `mut self: &mut Self`
 LL |         (&mut self).bar(); //~ ERROR cannot borrow
    |               ^^^^ cannot borrow mutably
+help: consider removing the `&mut`, as it is an immutable binding to a mutable reference
+   |
+LL |         self.bar(); //~ ERROR cannot borrow
+   |         ^^^^
 
 error: aborting due to 2 previous errors