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 rust-lang/rust#152024
r? compiler
This commit is contained in:
Stuart Cook
2026-02-08 16:58:24 +11:00
committed by GitHub
3 changed files with 43 additions and 0 deletions
+11
View File
@@ -1205,6 +1205,17 @@ fn access_place(
"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;
}
@@ -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);
}
}
@@ -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`.