Skip to content

Conversation

@profetia
Copy link
Contributor

@profetia profetia commented Oct 30, 2025

Closes #15968

changelog: [replace_box] fix FP when the box is moved

r? samueltardieu As you wish

Summary Notes

Managed by @rustbot—see help for details

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 30, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit concerned by the fact that we would be accumulating sets of HirId for every body in the crate while we don't need them after a body has been analyzed.

Since bodies can be stacked, maybe you can use a stack of caches instead of a large cache. This should be very lightweight. Also, you can use the BodyId from cx.enclosing_body more freely, to get the body's LocalDefId as well as the Body itself.

To make myself clear, I tried the proposed changes in samueltardieu@56b815e. Feel free to cherry-pick this commit if you agree and squash it with yours.

View changes since this review

@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@profetia
Copy link
Contributor Author

Looks good to me. Thank you!

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but can you please add those tests as well inside issue15951()?

    fn embedded_body() {
        let mut x = Box::new(());
        let y = x;
        x = Box::new(());

        struct Foo {
            inner: String,
        }
        let mut x = Box::new(Foo { inner: String::new() });
        let y = x.inner;
        x = Box::new(Foo { inner: String::new() });
        //~^ replace_box
    }

    let mut x = Box::new(Foo { inner: String::new() });
    let in_closure = || {
        x = Box::new(Foo { inner: String::new() });
        //~^ replace_box
    };

and this one outside any function:

static R: fn(&mut Box<String>) = |x| *x = Box::new(String::new());
//~^ replace_box

This will cover most test cases, and this is important since we are probably going to backport this fix to Rust 1.92.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 30, 2025
@samueltardieu
Copy link
Member

@rustbot label beta-nominated
@rustbot note Beta-nomination

The issue reported in #15968 affects Rust 1.92 which is the release in which this lint has been introduced. Backporting this fix would prevent the issue from appearing in stable Rust as the same time as the lint.

@rustbot rustbot added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Oct 30, 2025
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Oct 30, 2025
@samueltardieu
Copy link
Member

@y21 Would you do a second review since the PR is beta nominated?

@samueltardieu samueltardieu requested a review from y21 October 30, 2025 18:18
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

github-actions bot commented Oct 31, 2025

Lintcheck changes for 098ded3

Lint Added Removed Changed
clippy::replace_box 0 3 0

This comment will be updated if you push new changes

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you apply Jarcho's suggestion at https://github.com/rust-lang/rust-clippy/pull/15984/files#r2480316068 which is even better than a stack? This will clear the set when the uppermost level that used it is left so it is enough not to accumulate data for the whole crate.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 31, 2025
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Oct 31, 2025
@profetia
Copy link
Contributor Author

Updated. Thank you all!

@profetia profetia force-pushed the issue15968 branch 2 times, most recently from fd85896 to 2e20892 Compare November 1, 2025 00:27
@samueltardieu
Copy link
Member

@profetia Before I merge this, can you add a test with the double Box?

That will also let a last chance for people to comment on.

@samueltardieu
Copy link
Member

Also, it looks like we get false negatives now (see the lintcheck results). Could you have a look?

@Jarcho
Copy link
Contributor

Jarcho commented Nov 1, 2025

The current approach will have lots of false positives since it doesn't take temporal information into account. This needs a MIR analysis to work properly, but that transition is a much larger undertaking and it's not needed to get the lint out of the nursery.

A basic false negative would look like:

let mut x = Box::new(String::new());
x = Box::new(String::new());
x;

Any number of arbitrarily complex cases can be constructed that will be a huge pain to deal with when working with the HIR tree.

@samueltardieu
Copy link
Member

The current approach will have lots of false positives since it doesn't take temporal information into account

Do you mean FN? If yes, I agree.

@Jarcho
Copy link
Contributor

Jarcho commented Nov 1, 2025

That is indeed what I meant.

@profetia
Copy link
Contributor Author

profetia commented Nov 2, 2025

I agree too. I've add the test case and comment to describe it

@samueltardieu
Copy link
Member

Thanks.

@samueltardieu samueltardieu added this pull request to the merge queue Nov 2, 2025
Merged via the queue into rust-lang:master with commit e121ab8 Nov 2, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-nominated Nominated for backporting to the compiler in the beta channel.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False Positive replace_box about moved value

6 participants