Skip to content

Box::leak: tell people to avoid unleaking - #160323

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:no-unleak
Aug 1, 2026
Merged

Box::leak: tell people to avoid unleaking#160323
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
RalfJung:no-unleak

Conversation

@RalfJung

@RalfJung RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member

r? @nia-e
Cc @rust-lang/opsem

Note that this goes against the advice given by clippy in rust-lang/rust-clippy#17336. I think clippy should be adjusted to recommend Box::into_non_null instead. @ArhanChaudhary wold be great if you could make a clippy PR for that. :)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 1, 2026
@ArhanChaudhary

Copy link
Copy Markdown
Contributor

Do you think the lint should match against both unsafe { NonNull::new_unchecked(Box::into_raw(..)) } and NonNull::from_mut(Box::leak(..))?

@RalfJung

RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member Author

Yes I think that would be good.

@nia-e

nia-e commented Aug 1, 2026

Copy link
Copy Markdown
Member

Would it be useful to clarify under what conditions unleaking is acceptable? I can't think of any phrasing around allocators that doesn't sound overly long and confusing personally but maybe there is something. otherwise, lgtm

@RalfJung

RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member Author

I think it would have to be something like

  • the pointer passed to Box::from_raw must be derived from this mutable reference without any intervening field/element projections, and
  • the allocator must be either Global or a NativeAllocator

Up to you if you want to document that, IMO we are better off telling people not to do this even if it is sometimes technically allowed.

@nia-e

nia-e commented Aug 1, 2026

Copy link
Copy Markdown
Member

I like the new wording, we can let people dig into docs if they want to do more. ty ^^

@bors r+ rollup

@rust-bors

rust-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit bb24b38 has been approved by nia-e

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 1, 2026
@ArhanChaudhary

Copy link
Copy Markdown
Contributor

Hey, I have just opened rust-lang/rust-clippy#17485 :)

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 1, 2026
Box::leak: tell people to avoid unleaking

r? @nia-e
Cc @rust-lang/opsem

Note that this goes against the advice given by clippy in rust-lang/rust-clippy#17336. I think clippy should be adjusted to recommend `Box::into_non_null` instead. @ArhanChaudhary wold be great if you could make a clippy PR for that. :)
rust-bors Bot pushed a commit that referenced this pull request Aug 1, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
 - #160012 (miri: ensure validity of references and pointers we dereference and cast)
 - #160294 (Update Enzyme to resolve one of the open bugs)
 - #159503 (allocations: document that they can be read-only)
 - #160250 (When issuing suggestions for missing trait items, label unstable items)
 - #160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
 - #160311 (Remove final use of sealed traits from stdlib)
 - #160313 (Make the noundef-on-Cast size guard explicit)
 - #160323 (Box::leak: tell people to avoid unleaking)
 - #160328 (Move `check_track_caller` into the attribute parser)
rust-bors Bot pushed a commit that referenced this pull request Aug 1, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
 - #160012 (miri: ensure validity of references and pointers we dereference and cast)
 - #160294 (Update Enzyme to resolve one of the open bugs)
 - #159503 (allocations: document that they can be read-only)
 - #160179 (std: Update `wasip3` crate dependency)
 - #160250 (When issuing suggestions for missing trait items, label unstable items)
 - #160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
 - #160311 (Remove final use of sealed traits from stdlib)
 - #160313 (Make the noundef-on-Cast size guard explicit)
 - #160323 (Box::leak: tell people to avoid unleaking)
 - #160328 (Move `check_track_caller` into the attribute parser)
 - #160333 (Remove itertools dependency from `rustc_ast_pretty`)
@rust-bors
rust-bors Bot merged commit 878336c into rust-lang:main Aug 1, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 1, 2026
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Aug 2, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#157572 (stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw)
 - rust-lang/rust#160012 (miri: ensure validity of references and pointers we dereference and cast)
 - rust-lang/rust#160294 (Update Enzyme to resolve one of the open bugs)
 - rust-lang/rust#159503 (allocations: document that they can be read-only)
 - rust-lang/rust#160179 (std: Update `wasip3` crate dependency)
 - rust-lang/rust#160250 (When issuing suggestions for missing trait items, label unstable items)
 - rust-lang/rust#160251 (Replace unsafe usage of `NonNull::new_unchecked` with `Box::into_non_null`)
 - rust-lang/rust#160311 (Remove final use of sealed traits from stdlib)
 - rust-lang/rust#160313 (Make the noundef-on-Cast size guard explicit)
 - rust-lang/rust#160323 (Box::leak: tell people to avoid unleaking)
 - rust-lang/rust#160328 (Move `check_track_caller` into the attribute parser)
 - rust-lang/rust#160333 (Remove itertools dependency from `rustc_ast_pretty`)
@RalfJung
RalfJung deleted the no-unleak branch August 2, 2026 16:46
@BoxyUwU BoxyUwU added the relnotes Marks issues that should be documented in the release notes of the next release. label Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants