Skip to content

Commit 5405e5d

Browse files
committed
Suppress unused_mut lint if mutation fails due to borrowck error
Remedying the borrowck error will likely result in the mut becoming used.
1 parent 46c86ae commit 5405e5d

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

compiler/rustc_borrowck/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
12071207
"access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
12081208
place_span, kind
12091209
);
1210+
1211+
// If the place is being mutated, then mark it as such anyway in order to suppress the
1212+
// `unused_mut` lint, which is likely incorrect once the access place error has been
1213+
// resolved.
1214+
if rw == ReadOrWrite::Write(WriteKind::Mutate)
1215+
&& let Ok(root_place) =
1216+
self.is_mutable(place_span.0.as_ref(), is_local_mutation_allowed)
1217+
{
1218+
self.add_used_mut(root_place, state);
1219+
}
1220+
12101221
return;
12111222
}
12121223

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Do not fire unused_mut lint when mutation of the bound variable fails due to a borrow-checking
2+
//! error.
3+
//!
4+
//! Regression test for https://github.com/rust-lang/rust/issues/152024
5+
//@ compile-flags: -W unused_mut
6+
7+
struct Thing;
8+
impl Drop for Thing {
9+
fn drop(&mut self) {}
10+
}
11+
12+
fn main() {
13+
let mut t;
14+
let mut b = None;
15+
loop {
16+
t = Thing; //~ ERROR cannot assign to `t` because it is borrowed
17+
b.insert(&t);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0506]: cannot assign to `t` because it is borrowed
2+
--> $DIR/mut-used-despite-borrowck-error.rs:16:9
3+
|
4+
LL | t = Thing;
5+
| ^ `t` is assigned to here but it was already borrowed
6+
LL | b.insert(&t);
7+
| - -- `t` is borrowed here
8+
| |
9+
| borrow later used here
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0506`.

0 commit comments

Comments
 (0)