-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New lint: nonnull_unchecked_on_box_ptr
#17336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArhanChaudhary
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
ArhanChaudhary:lint-nonnull_unchecked_on_box_ptr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| use clippy_config::Conf; | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use clippy_utils::msrvs::{self, Msrv}; | ||
| use clippy_utils::res::{MaybeDef, MaybeQPath}; | ||
| use clippy_utils::source::snippet_with_context; | ||
| use clippy_utils::sym; | ||
| use clippy_utils::visitors::is_expr_unsafe; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, UnsafeSource}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_session::impl_lint_pass; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for unsafe usage of `NonNull::new_unchecked(Box::into_raw(x))`, and suggests calling `NonNull::from_mut(Box::leak(x))` instead. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// `NonNull::new_unchecked` is an unsafe function, which we don't need to call at all if we can instead use a mutable reference. | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// use std::ptr::NonNull; | ||
| /// let one = Box::new(1); | ||
| /// let ptr = unsafe { NonNull::new_unchecked(Box::into_raw(one)) }; | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// use std::ptr::NonNull; | ||
| /// let one = Box::new(1); | ||
| /// let ptr = NonNull::from_mut(Box::leak(one)); | ||
| /// ``` | ||
| #[clippy::version = "1.98.0"] | ||
| pub NONNULL_UNCHECKED_ON_BOX_PTR, | ||
| complexity, | ||
| "using `NonNull::new_unchecked` with `Box::into_raw`, while `NonNull::from_mut` with `Box::leak` can be used instead" | ||
| } | ||
|
|
||
| impl_lint_pass!(NonnullUncheckedOnBoxPtr => [NONNULL_UNCHECKED_ON_BOX_PTR]); | ||
|
|
||
| pub struct NonnullUncheckedOnBoxPtr { | ||
| msrv: Msrv, | ||
| } | ||
|
|
||
| impl NonnullUncheckedOnBoxPtr { | ||
| pub fn new(conf: &Conf) -> Self { | ||
| Self { msrv: conf.msrv } | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for NonnullUncheckedOnBoxPtr { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
| if !expr.span.from_expansion() | ||
| && let ExprKind::Call(nonnull_new_unchecked, [arg]) = expr.kind | ||
| && let ExprKind::Call(box_into_raw, [arg]) = arg.kind | ||
| && nonnull_new_unchecked | ||
| .ty_rel_def_if_named(cx, sym::new_unchecked) | ||
| .opt_parent(cx) | ||
| .opt_impl_ty(cx) | ||
| .is_diag_item(cx, sym::NonNull) | ||
| && box_into_raw | ||
| .ty_rel_def_if_named(cx, sym::into_raw) | ||
| .opt_parent(cx) | ||
| .opt_impl_ty(cx) | ||
| .is_lang_item(cx, LangItem::OwnedBox) | ||
| && self.msrv.meets(cx, msrvs::BOX_LEAK) | ||
| { | ||
| let ctxt = expr.span.ctxt(); | ||
| let span = match cx.tcx.parent_hir_node(expr.hir_id) { | ||
| Node::Block(&Block { | ||
| rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), | ||
| span: unsafe_span, | ||
| stmts, | ||
| .. | ||
| }) if unsafe_span.ctxt() == ctxt && !is_expr_unsafe(cx, arg) && stmts.is_empty() => unsafe_span, | ||
| _ => expr.span, | ||
| }; | ||
|
|
||
| span_lint_and_then( | ||
| cx, | ||
| NONNULL_UNCHECKED_ON_BOX_PTR, | ||
| span, | ||
| "use of `NonNull::new_unchecked` with `Box::into_raw`", | ||
| |diag| { | ||
| let mut app = Applicability::MachineApplicable; | ||
| let arg_name = snippet_with_context(cx, arg.span, ctxt, "_", &mut app).0; | ||
|
|
||
| let sugg = if self.msrv.meets(cx, msrvs::NONNULL_FROM_MUT) { | ||
| format!("NonNull::from_mut(Box::leak({arg_name}))") | ||
| } else { | ||
| format!("NonNull::from(Box::leak({arg_name}))") | ||
| }; | ||
|
|
||
| diag.span_suggestion(span, "try", sugg, app); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| #![warn(clippy::nonnull_unchecked_on_box_ptr)] | ||
|
|
||
| use std::ptr::NonNull; | ||
|
|
||
| macro_rules! identity { | ||
| ($x:expr) => { | ||
| $x | ||
| }; | ||
| } | ||
|
|
||
| macro_rules! weird { | ||
| ($x:expr) => {{ | ||
| let y = 1; | ||
| $x | ||
| }}; | ||
| } | ||
|
|
||
| macro_rules! from_macro { | ||
| ($x:expr) => { | ||
| unsafe { NonNull::new_unchecked(Box::into_raw($x)) } | ||
| }; | ||
| } | ||
|
|
||
| fn identity<T>(x: T) -> T { | ||
| x | ||
| } | ||
|
|
||
| unsafe fn unsafe_identity<T>(x: T) -> T { | ||
| x | ||
| } | ||
|
|
||
| fn lint() { | ||
| fn basic() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(identity(one))); | ||
| } | ||
|
|
||
| fn qualifiers() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| { | ||
| use Box as Box2; | ||
| use NonNull as NonNull2; | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
| } | ||
|
|
||
| { | ||
| type Box2<T> = Box<T>; | ||
| type NonNull2<T> = NonNull<T>; | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
| } | ||
| } | ||
|
|
||
| fn macros() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = identity!(NonNull::from_mut(Box::leak(one))); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(weird!(one))); | ||
| } | ||
|
|
||
| fn keep_unsafe_block() { | ||
| let one = Box::new(1); | ||
| let _ = unsafe { | ||
| NonNull::from_mut(Box::leak(unsafe_identity(one))) | ||
| //~^ nonnull_unchecked_on_box_ptr | ||
| }; | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = unsafe { | ||
| identity(NonNull::from_mut(Box::leak(one))) | ||
| //~^ nonnull_unchecked_on_box_ptr | ||
| }; | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = unsafe { | ||
| unsafe_identity(NonNull::from_mut(Box::leak(one))) | ||
| //~^ nonnull_unchecked_on_box_ptr | ||
| }; | ||
|
|
||
| let _ = unsafe { | ||
| NonNull::from_mut(Box::leak(Box::new(std::num::NonZeroI32::new_unchecked(1)))) | ||
| //~^ nonnull_unchecked_on_box_ptr | ||
| }; | ||
|
|
||
| let _ = unsafe { | ||
| let one = Box::new(1); | ||
| NonNull::from_mut(Box::leak(one)) | ||
| //~^ nonnull_unchecked_on_box_ptr | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| fn no_lint() { | ||
| fn basic() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
|
|
||
| let one = Box::new(1); | ||
| let _ = unsafe { NonNull::new_unchecked(identity(Box::into_raw(one))) }; | ||
| } | ||
|
|
||
| // TODO? | ||
| fn does_not_check_expr_init() { | ||
| let one = Box::new(1); | ||
| let leaked = Box::into_raw(one); | ||
| let _ = unsafe { NonNull::new_unchecked(leaked) }; | ||
|
|
||
| let one = Box::new(1); | ||
| let leaked = Box::leak(one); | ||
| let _ = NonNull::from_mut(leaked); | ||
| } | ||
|
|
||
| fn macros() { | ||
| let one = Box::new(1); | ||
| let _ = from_macro!(one); | ||
| } | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.25"] | ||
| fn msrv_1_25() { | ||
| let one = Box::new(1); | ||
| let _ = unsafe { NonNull::new_unchecked(Box::into_raw(one)) }; | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.26"] | ||
| fn msrv_1_26() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from(Box::leak(one)); | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.89"] | ||
| fn msrv_1_89() { | ||
| let one = Box::new(1); | ||
| let _ = NonNull::from_mut(Box::leak(one)); | ||
| } | ||
|
|
||
| fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.