Skip to content

Commit 6dfed7e

Browse files
committed
small refactoring: replaced mutable state with return statements in control flow.
As a drive-by, removed some dead-code.
1 parent be645be commit 6dfed7e

File tree

1 file changed

+22
-15
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+22
-15
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17021702
"check_access_permissions({:?}, {:?}, {:?})",
17031703
place, kind, is_local_mutation_allowed
17041704
);
1705-
let mut error_reported = false;
1705+
17061706
match kind {
17071707
Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Unique))
17081708
| Reservation(WriteKind::MutableBorrow(borrow_kind @ BorrowKind::Mut { .. }))
@@ -1715,9 +1715,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17151715
BorrowKind::Shared => unreachable!(),
17161716
};
17171717
match self.is_mutable(place, is_local_mutation_allowed) {
1718-
Ok(root_place) => self.add_used_mut(root_place, flow_state),
1718+
Ok(root_place) => {
1719+
self.add_used_mut(root_place, flow_state);
1720+
return false;
1721+
}
17191722
Err(place_err) => {
1720-
error_reported = true;
17211723
let item_msg = self.get_default_err_msg(place);
17221724
let mut err = self.tcx
17231725
.cannot_borrow_path_as_mutable(span, &item_msg, Origin::Mir);
@@ -1731,15 +1733,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17311733
}
17321734

17331735
err.emit();
1736+
return true;
17341737
}
17351738
}
17361739
}
17371740
Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
17381741
match self.is_mutable(place, is_local_mutation_allowed) {
1739-
Ok(root_place) => self.add_used_mut(root_place, flow_state),
1742+
Ok(root_place) => {
1743+
self.add_used_mut(root_place, flow_state);
1744+
return false;
1745+
}
17401746
Err(place_err) => {
1741-
error_reported = true;
1742-
17431747
let err_info = if let Place::Projection(
17441748
box Projection {
17451749
base: Place::Local(local),
@@ -1748,11 +1752,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17481752
) = *place_err {
17491753
let locations = self.mir.find_assignments(local);
17501754
if locations.len() > 0 {
1751-
let item_msg = if error_reported {
1752-
self.get_secondary_err_msg(&Place::Local(local))
1753-
} else {
1754-
self.get_default_err_msg(place)
1755-
};
1755+
let item_msg = self.get_secondary_err_msg(&Place::Local(local));
17561756
let sp = self.mir.source_info(locations[0]).span;
17571757
let mut to_suggest_span = String::new();
17581758
if let Ok(src) =
@@ -1797,6 +1797,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17971797
}
17981798
err.emit();
17991799
}
1800+
1801+
return true;
18001802
}
18011803
}
18021804
}
@@ -1815,15 +1817,20 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18151817
),
18161818
);
18171819
}
1820+
return false;
1821+
}
1822+
Activation(..) => {
1823+
// permission checks are done at Reservation point.
1824+
return false;
18181825
}
1819-
Activation(..) => {} // permission checks are done at Reservation point.
18201826
Read(ReadKind::Borrow(BorrowKind::Unique))
18211827
| Read(ReadKind::Borrow(BorrowKind::Mut { .. }))
18221828
| Read(ReadKind::Borrow(BorrowKind::Shared))
1823-
| Read(ReadKind::Copy) => {} // Access authorized
1829+
| Read(ReadKind::Copy) => {
1830+
// Access authorized
1831+
return false;
1832+
}
18241833
}
1825-
1826-
error_reported
18271834
}
18281835

18291836
/// Adds the place into the used mutable variables set

0 commit comments

Comments
 (0)