Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
"access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
place_span, kind
);

// If the place is being mutated, then mark it as such anyway in order to suppress the
// `unused_mut` lint, which is likely incorrect once the access place error has been
// resolved.
if rw == ReadOrWrite::Write(WriteKind::Mutate)
&& let Ok(root_place) =
self.is_mutable(place_span.0.as_ref(), is_local_mutation_allowed)
{
self.add_used_mut(root_place, state);
}

return;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/ui/lint/unused/mut-used-despite-borrowck-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Do not fire unused_mut lint when mutation of the bound variable fails due to a borrow-checking
//! error.
//!
//! Regression test for https://github.com/rust-lang/rust/issues/152024
//@ compile-flags: -W unused_mut

struct Thing;
impl Drop for Thing {
fn drop(&mut self) {}
}

fn main() {
let mut t;
let mut b = None;
loop {
t = Thing; //~ ERROR cannot assign to `t` because it is borrowed
b.insert(&t);
}
}
13 changes: 13 additions & 0 deletions tests/ui/lint/unused/mut-used-despite-borrowck-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0506]: cannot assign to `t` because it is borrowed
--> $DIR/mut-used-despite-borrowck-error.rs:16:9
|
LL | t = Thing;
| ^ `t` is assigned to here but it was already borrowed
LL | b.insert(&t);
| - -- `t` is borrowed here
| |
| borrow later used here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0506`.
Loading