Skip to content

Commit abdb64d

Browse files
committed
Check equivalence of indices in more cases
1 parent ad6ae61 commit abdb64d

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21162116
None
21172117
}
21182118
}) else {
2119-
note_default_suggestion();
2119+
let hir::Node::Expr(parent) = tcx.parent_hir_node(index1.hir_id) else { return };
2120+
let hir::ExprKind::Index(_, idx1, _) = parent.kind else { return };
2121+
let hir::Node::Expr(parent) = tcx.parent_hir_node(index2.hir_id) else { return };
2122+
let hir::ExprKind::Index(_, idx2, _) = parent.kind else { return };
2123+
if !idx1.equals(idx2) {
2124+
err.help("use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices");
2125+
}
21202126
return;
21212127
};
21222128

tests/ui/suggestions/suggest-split-at-mut.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ LL | let b = &mut foo[3];
88
LL | *a = 5;
99
| ------ first borrow later used here
1010
|
11-
= help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
12-
= help: consider using `.swap(index_1, index_2)` to swap elements at the specified indices
11+
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices
1312

1413
error[E0499]: cannot borrow `foo` as mutable more than once at a time
1514
--> $DIR/suggest-split-at-mut.rs:13:18
@@ -59,8 +58,7 @@ LL | *b = 6;
5958
LL | println!("{:?} {:?}", a, b);
6059
| - immutable borrow later used here
6160
|
62-
= help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
63-
= help: consider using `.swap(index_1, index_2)` to swap elements at the specified indices
61+
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices
6462

6563
error[E0502]: cannot borrow `foo[_]` as immutable because it is also borrowed as mutable
6664
--> $DIR/suggest-split-at-mut.rs:46:13
@@ -72,8 +70,7 @@ LL | let b = &foo[2];
7270
LL | *a = 5;
7371
| ------ mutable borrow later used here
7472
|
75-
= help: consider using `.split_at_mut(position)` or similar method to obtain two mutable non-overlapping sub-slices
76-
= help: consider using `.swap(index_1, index_2)` to swap elements at the specified indices
73+
= help: use `.split_at_mut(position)` to obtain two mutable non-overlapping sub-slices
7774

7875
error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
7976
--> $DIR/suggest-split-at-mut.rs:54:14

0 commit comments

Comments
 (0)