Skip to content

False Positive replace_box about moved value #15968

@tisonkun

Description

@tisonkun

Summary

Suggest moved box can be replaced in place. Maybe related to quote!.

    let mut item_fn = item_fn;
    let block = item_fn.block;
    let ret = match &item_fn.sig.output {
        // impl trait is not supported in closure return type, override with
        // default, which is inferring.
        ReturnType::Type(_, ty) if matches!(**ty, Type::ImplTrait(_)) => ReturnType::Default,
        _ => item_fn.sig.output.clone(),
    };

    let stacksafe_crate = crate_path.unwrap_or_else(|| parse_quote!(::stacksafe));

    let wrapped_block = quote! {
        {
            #stacksafe_crate::internal::stacker::maybe_grow(
                #stacksafe_crate::get_minimum_stack_size(),
                #stacksafe_crate::get_stack_allocation_size(),
                #stacksafe_crate::internal::with_protected(move || #ret { #block })
            )
        }
    };
    item_fn.block = Box::new(syn::parse(wrapped_block.into()).unwrap());
    item_fn.into_token_stream().into()

Lint Name

replace_box

Reproducer

Cannot make a minimal reproducer:

warning: failed to automatically apply fixes suggested by rustc to crate `stacksafe_macro`

after fixes were automatically applied the compiler reported errors within these files:

  * stacksafe-macro/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0382]: use of moved value: `item_fn.block`
  --> stacksafe-macro/src/lib.rs:85:5
   |
66 |     let block = item_fn.block;
   |                 ------------- value moved here
...
85 |     *item_fn.block = syn::parse(wrapped_block.into()).unwrap();
   |     ^^^^^^^^^^^^^^ value used here after move
   |
   = note: move occurs because `item_fn.block` has type `std::boxed::Box<syn::Block>`, which does not implement the `Copy` trait

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.

warning: creating a new box
  --> stacksafe-macro/src/lib.rs:85:5
   |
85 |     item_fn.block = Box::new(syn::parse(wrapped_block.into()).unwrap());
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*item_fn.block = syn::parse(wrapped_block.into()).unwrap()`
   |
   = note: this creates a needless allocation
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#replace_box
   = note: `#[warn(clippy::replace_box)]` on by default

warning: `stacksafe-macro` (lib test) generated 1 warning (run `cargo clippy --fix --lib -p stacksafe-macro --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `stacksafe_macro`

after fixes were automatically applied the compiler reported errors within these files:

  * stacksafe-macro/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0382]: use of moved value: `item_fn.block`
  --> stacksafe-macro/src/lib.rs:85:5
   |
66 |     let block = item_fn.block;
   |                 ------------- value moved here
...
85 |     *item_fn.block = syn::parse(wrapped_block.into()).unwrap();
   |     ^^^^^^^^^^^^^^ value used here after move
   |
   = note: move occurs because `item_fn.block` has type `std::boxed::Box<syn::Block>`, which does not implement the `Copy` trait

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.

warning: `stacksafe-macro` (lib) generated 1 warning (1 duplicate)
warning: failed to automatically apply fixes suggested by rustc to crate `stacksafe_macro`

after fixes were automatically applied the compiler reported errors within these files:

  * stacksafe-macro/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0382]: use of moved value: `item_fn.block`
  --> stacksafe-macro/src/lib.rs:85:5
   |
66 |     let block = item_fn.block;
   |                 ------------- value moved here
...
85 |     *item_fn.block = syn::parse(wrapped_block.into()).unwrap();
   |     ^^^^^^^^^^^^^^ value used here after move
   |
   = note: move occurs because `item_fn.block` has type `std::boxed::Box<syn::Block>`, which does not implement the `Copy` trait

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.

Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
release: 1.90.0
LLVM version: 20.1.8

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions