Skip to content

Commit 8e16ea8

Browse files
authored
Rollup merge of #152037 - eggyal:unused-mut-due-to-borrowck-error, r=jackh726
Suppress unused_mut lint if mutation fails due to borrowck error Remedying the borrowck error will likely result in the mut becoming used, and therefore the lint is likely incorrect. Fixes #152024 r? compiler
2 parents 68f4a99 + 5405e5d commit 8e16ea8

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
@@ -1205,6 +1205,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
12051205
"access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
12061206
place_span, kind
12071207
);
1208+
1209+
// If the place is being mutated, then mark it as such anyway in order to suppress the
1210+
// `unused_mut` lint, which is likely incorrect once the access place error has been
1211+
// resolved.
1212+
if rw == ReadOrWrite::Write(WriteKind::Mutate)
1213+
&& let Ok(root_place) =
1214+
self.is_mutable(place_span.0.as_ref(), is_local_mutation_allowed)
1215+
{
1216+
self.add_used_mut(root_place, state);
1217+
}
1218+
12081219
return;
12091220
}
12101221

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)