|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::res::{MaybeDef as _, MaybeQPath as _}; |
| 5 | +use clippy_utils::source::snippet_with_context; |
| 6 | +use clippy_utils::sym; |
| 7 | +use clippy_utils::visitors::is_expr_unsafe; |
| 8 | +use rustc_errors::Applicability; |
| 9 | +use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, UnsafeSource}; |
| 10 | +use rustc_lint::{LateContext, LateLintPass}; |
| 11 | +use rustc_session::impl_lint_pass; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for unsafe usage of `NonNull::new_unchecked(Box::into_raw(x))`, and suggests calling `NonNull::from_mut(Box::leak(x))` instead. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// `NonNull::new_unchecked` is an unsafe function, which we don't need to call at all if we can instead use a mutable reference. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```no_run |
| 22 | + /// use std::ptr::NonNull; |
| 23 | + /// let one = Box::new(1); |
| 24 | + /// let ptr = unsafe { NonNull::new_unchecked(Box::into_raw(one)) }; |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```no_run |
| 28 | + /// use std::ptr::NonNull; |
| 29 | + /// let one = Box::new(1); |
| 30 | + /// let ptr = NonNull::from_mut(Box::leak(one)); |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.98.0"] |
| 33 | + pub NONNULL_UNCHECKED_ON_BOX_PTR, |
| 34 | + complexity, |
| 35 | + "using `NonNull::new_unchecked` with `Box::into_raw`, while `NonNull::from_mut` with `Box::leak` can be used instead" |
| 36 | +} |
| 37 | + |
| 38 | +impl_lint_pass!(NonnullUncheckedOnBoxPtr => [NONNULL_UNCHECKED_ON_BOX_PTR]); |
| 39 | + |
| 40 | +pub struct NonnullUncheckedOnBoxPtr { |
| 41 | + msrv: Msrv, |
| 42 | +} |
| 43 | + |
| 44 | +impl NonnullUncheckedOnBoxPtr { |
| 45 | + pub fn new(conf: &Conf) -> Self { |
| 46 | + Self { msrv: conf.msrv } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'tcx> LateLintPass<'tcx> for NonnullUncheckedOnBoxPtr { |
| 51 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 52 | + if !expr.span.from_expansion() |
| 53 | + && let ExprKind::Call(nonnull_new_unchecked, [arg]) = expr.kind |
| 54 | + && let ExprKind::Call(box_into_raw, [arg]) = arg.kind |
| 55 | + && nonnull_new_unchecked |
| 56 | + .ty_rel_def_if_named(cx, sym::new_unchecked) |
| 57 | + .opt_parent(cx) |
| 58 | + .opt_impl_ty(cx) |
| 59 | + .is_diag_item(cx, sym::NonNull) |
| 60 | + && box_into_raw |
| 61 | + .ty_rel_def_if_named(cx, sym::into_raw) |
| 62 | + .opt_parent(cx) |
| 63 | + .opt_impl_ty(cx) |
| 64 | + .is_lang_item(cx, LangItem::OwnedBox) |
| 65 | + && self.msrv.meets(cx, msrvs::BOX_LEAK) |
| 66 | + { |
| 67 | + let ctxt = expr.span.ctxt(); |
| 68 | + let span = match cx.tcx.parent_hir_node(expr.hir_id) { |
| 69 | + Node::Block(&Block { |
| 70 | + rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), |
| 71 | + span: unsafe_span, |
| 72 | + stmts, |
| 73 | + .. |
| 74 | + }) if unsafe_span.ctxt() == ctxt && !is_expr_unsafe(cx, arg) && stmts.is_empty() => unsafe_span, |
| 75 | + _ => expr.span, |
| 76 | + }; |
| 77 | + |
| 78 | + span_lint_and_then( |
| 79 | + cx, |
| 80 | + NONNULL_UNCHECKED_ON_BOX_PTR, |
| 81 | + span, |
| 82 | + "use of `NonNull::new_unchecked` with `Box::into_raw`", |
| 83 | + |diag| { |
| 84 | + let mut app = Applicability::MachineApplicable; |
| 85 | + let arg_name = snippet_with_context(cx, arg.span, ctxt, "_", &mut app).0; |
| 86 | + |
| 87 | + let sugg = if self.msrv.meets(cx, msrvs::NONNULL_FROM_MUT) { |
| 88 | + format!("NonNull::from_mut(Box::leak({arg_name}))") |
| 89 | + } else { |
| 90 | + format!("NonNull::from(Box::leak({arg_name}))") |
| 91 | + }; |
| 92 | + |
| 93 | + diag.span_suggestion(span, "try", sugg, app); |
| 94 | + }, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments