clippy::replace_box suggests a MachineApplicable fix that is semantically questionable for Box<&mut T>
Summary
Hi, thanks for the great tool!
Clippy’s replace_box lint flags Box::new(...) assignments to an existing Box
Reproducer
fn main() {
let mut raphael = 300;
let mut samuel = Box::new(&mut raphael);
**samuel += 500;
let mut albert = 900;
samuel = Box::new(&mut albert);
}
Clippy output
warning: creating a new box
--> src/main.rs:9:5
|
9 | samuel = Box::new(&mut albert);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*samuel = &mut albert`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#replace_box
Issue The two forms are not equivalent here:
// original (linted)
samuel = Box::new(&mut albert);
// suggested fix
*samuel = &mut albert;
Both assign a mutable reference into the Box, but the lint fires only on the Box::new(...) spelling and suggests rewriting it to the deref-assignment form. For Box<&mut T> this ignores the reference’s lifetime implications; the deref-assign form can behave differently (or even be rejected by the borrow checker) depending on the lifetimes involved. Since the suggestion is marked MachineApplicable, a user applying it may silently change semantics or end up with a borrow-check error for code that previously compiled.
Expected behavior The suggestion should either:
be restricted to cases where the inner type is not itself a reference (or is otherwise lifetime-free), or not be emitted as MachineApplicable for Box<&mut T>.
Version
Additional Labels
No response
I had a go at reproducing a divergence here and could not find one. Compiling both spellings on nightly-2026-08-07 (rustc 1.99.0-nightly, 84b36a78a): the snippet above, an old borrow used after the reassignment, a shorter-lived new borrow, an explicit-lifetime function boundary, an RHS reborrowing the box's own content, a live reborrow held across the assignment, a struct field with a lifetime parameter, an invariant Box<&mut &mut i32>, and a closure holding the box. All nine are accepted or rejected the same way either side, and the snippet above prints 900 in both forms.
The closure case is the only rejection, and it fails both ways with the same error:
let mut a = 1;
let mut c = 2;
let mut bx = Box::new(&mut a);
let cl = || { let _ = &bx; };
bx = Box::new(&mut c); // E0506: cannot assign to `bx` because it is borrowed
// *bx = &mut c; // E0506: cannot assign to `*bx` because it is borrowed
cl();
That lines up with what the types ask for: Box<T> is covariant, so &'b mut T has to outlive the same region whether it is written into the existing box or wrapped in a new one, and the obligation does not move between the two.
What does change is allocation identity, since the old box is freed rather than reused, but that holds for every Box<T> and is the thing the lint is about.
Happy to be shown a case that does diverge.
They're different in how long the first borrow is held.
let mut a = 0;
let mut b = 0;
let mut c = Box::new(&mut a);
c = Box::new(&mut b); // The borrow of `a` is now dead.
a =1;
*c; // removing this will cause the contents of the box to be considered dead earlier.
If you use *c = &mut b it will leave the borrow of a live and you'll get a conflict when trying to modify a.
I'd like to take this. Plan is to keep the lint but drop the suggestion to MaybeIncorrect for Box<&mut _>: *b = &mut x holds the box's borrow across the assignment, unlike b = Box::new(&mut x), so it shouldn't be auto-applied. PR shortly.
@rustbot claim
Thank you very much for taking the time to review this report.
I’d like to work on this issue.
I plan to make the replace_box suggestion more conservative for Box<&mut T> so that Clippy does not mark a potentially semantically questionable transformation as MachineApplicable.
I’ll add a regression test covering the Box<&mut T> case and verify that the existing behavior and suggestions for other Box<T> cases remain unchanged.
GitHub: https://www.github.com/sanskarIN