rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

clippy::replace_box suggests a MachineApplicable fix that is semantically questionable for Box<&mut T>

Open HinataDeveloper opened this issue 3 months ago • 5 comments

Summary

Hi, thanks for the great tool!

Clippy’s replace_box lint flags Box::new(...) assignments to an existing Box and suggests writing to the inner value instead. The suggestion is marked MachineApplicable, but in the case where the boxed type is itself a mutable reference (Box<&mut T>), the suggested fix is semantically different from the original code and can be misleading regarding borrow/lifetime behavior.

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

HinataDeveloper avatar Aug 12 '26 03:08 HinataDeveloper

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.

wilyan09007 avatar Aug 12 '26 07:08 wilyan09007

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.

Jarcho avatar Aug 12 '26 16:08 Jarcho

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

DoTuanAnh2k1 avatar Aug 12 '26 18:08 DoTuanAnh2k1

Thank you very much for taking the time to review this report.

HinataDeveloper avatar Aug 13 '26 02:08 HinataDeveloper

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

sanskarIN avatar Sep 20 '26 06:09 sanskarIN