Skip to content

Rename and reimplement nonnull_unchecked_on_box_ptr - #17485

Open
ArhanChaudhary wants to merge 10 commits into
rust-lang:masterfrom
ArhanChaudhary:revamp-my-lint
Open

Rename and reimplement nonnull_unchecked_on_box_ptr#17485
ArhanChaudhary wants to merge 10 commits into
rust-lang:masterfrom
ArhanChaudhary:revamp-my-lint

Conversation

@ArhanChaudhary

@ArhanChaudhary ArhanChaudhary commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

changelog: rename and reimplement nonnull_unchecked_on_box_ptr lint.

Address the discussion in rust-lang/rust#160251 with @RalfJung's recommendation of a re-implementation of the nonnull_unchecked_on_box_ptr lint. Now that rust-lang/rust#130364 was very recently stabilized, we can ship this lint with 1.98.

A question: because the old lint had not yet been released, do I need to follow cargo dev rename_lint? If so, do I need to specify #[clippy::version = "1.99.0"] (which doesn't make sense because 1.98 hasn't been released yet) or leave it as #[clippy::version = ""]? Currently, I have ran cargo dev rename_lint and specified #[clippy::version = ""].

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 1, 2026
@rustbot

rustbot commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 1, 2026
/// Checks for unsafe usage of `NonNull::new_unchecked(Box::into_raw(x))` or dangerous usage of `NonNull::from_mut(Box::leak(x))` and suggests calling `Box::into_non_null(x)` instead.
///
/// ### Why is this bad?
/// First, `NonNull::new_unchecked` is an unsafe function, which we don't need to call at all. Second, at the time of writing, whether or not you are allowed to reconstruct the `Box` from the mutable reference returned by `Box::leak` is an [open question](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.leak). Thus, this lint helps prevent future dangerous calls to `Box::from_non_null`.

@ArhanChaudhary ArhanChaudhary Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The hyperlink will eventually point to the updated documentation in this PR: rust-lang/rust#160323

View changes since the review

@ArhanChaudhary

Copy link
Copy Markdown
Contributor Author

Also, I'm not sure what category this lint falls into anymore, since the from_mut transformation is a correctness transformation but the new_unchecked transformation is a complexity transformation. We could separate these into two different lints, but that would feel weird. Let me know your thoughts.

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

Lintcheck changes for 3d65490

Lint Added Removed Changed
clippy::nonnull_unchecked_on_box_ptr 0 1 0

This comment will be updated if you push new changes

@rustbot

This comment has been minimized.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

community review: LGTM, mostly a move with the minor changes already discussed 👍🏻

View changes since this review

Comment thread tests/ui/improper_nonnull_from_box.rs Outdated

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above:
Community review LGTM

View changes since this review

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

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.

@Jarcho

Jarcho commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Nothing about NonNull::from(Box::leak(_)) is undefined or even wrong so it doesn't belong in correctness. This also makes improper_nonnull_from_box a very misleading name.

@ArhanChaudhary

Copy link
Copy Markdown
Contributor Author

There was recently discussion where the compiler people agreed that this is, in general dangerous, specifically with custom allocators. rust-lang/rust#160251 (comment)

See the PR description for more

@Jarcho

Jarcho commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

I read the thread. The issue there is unleaking the pointer created, not creating the pointer in general.

@ArhanChaudhary

Copy link
Copy Markdown
Contributor Author

The idea is to prevent future unleaking of the pointer, despite creating the pointer is safe.

@ArhanChaudhary

Copy link
Copy Markdown
Contributor Author

Or, apologies I mischaracterized what you were responding to. Did you bring that up to answer which lint category this should be in, or do you disagree with the rewrite?

@Jarcho

Jarcho commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

That neither justifies the rename nor claiming that it's a correctness issue. I have no issue linting on it.

@Jarcho Jarcho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This feels like it should be two different lints. Linting NonNull::from(Box::leak()) is only done due to unresolved provenance questions if that pointer is later unleaked. That definitely doesn't fit a complexity lint, but it could be either suspicious or pedantic depending on what the lint catches in practice.

View changes since this review

Comment on lines +100 to +104
&& expr2
.ty_rel_def_if_named(cx, sym::leak)
.opt_parent(cx)
.opt_impl_ty(cx)
.is_lang_item(cx, LangItem::OwnedBox)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This needs to check specifically for Box<T, Global>. You can suggest into_non_null_with_allocator for any other allocator.


declare_clippy_lint! {
/// ### What it does
/// Checks for unsafe usage of `NonNull::new_unchecked(Box::into_raw(x))` or dangerous usage of `NonNull::from_mut(Box::leak(x))` and suggests calling `Box::into_non_null(x)` instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can't be called a dangerous use since we didn't (and can't reasonably) check if it was unleaked.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 9, 2026
@rustbot

rustbot commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Aug 9, 2026
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17538) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants