forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect borrow error involving sub-slices and suggest
split_at_mut
``` error[E0499]: cannot borrow `foo` as mutable more than once at a time --> $DIR/suggest-split-at-mut.rs:13:18 | LL | let a = &mut foo[..2]; | --- first mutable borrow occurs here LL | let b = &mut foo[2..]; | ^^^ second mutable borrow occurs here LL | a[0] = 5; | ---- first borrow later used here | = help: use `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices ``` Address most of rust-lang#58792. For follow up work, we should emit a structured suggestion for cases where we can identify the exact `let (a, b) = foo.split_at_mut(2);` call that is needed.
- Loading branch information
Showing
4 changed files
with
57 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
fn main() { | ||
fn foo() { | ||
let mut foo = [1, 2, 3, 4]; | ||
let a = &mut foo[2]; | ||
let b = &mut foo[3]; //~ ERROR cannot borrow `foo[_]` as mutable more than once at a time | ||
*a = 5; | ||
*b = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn bar() { | ||
let mut foo = [1,2,3,4]; | ||
let a = &mut foo[..2]; | ||
let b = &mut foo[2..]; //~ ERROR cannot borrow `foo` as mutable more than once at a time | ||
a[0] = 5; | ||
b[0] = 6; | ||
println!("{:?} {:?}", a, b); | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
bar(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters