|
| 1 | +use clippy_config::Conf; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::msrvs::{self, Msrv}; |
| 4 | +use clippy_utils::res::{MaybeDef, MaybeResPath}; |
| 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, QPath, 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 | + // Parse `NonNull::new_unchecked` |
| 54 | + && let ExprKind::Call(nonnull_new_unchecked, [arg]) = expr.kind |
| 55 | + && let ExprKind::Path(QPath::TypeRelative(nonnnull, new_unchecked)) = nonnull_new_unchecked.kind |
| 56 | + && new_unchecked.ident.name == sym::new_unchecked |
| 57 | + && nonnnull.basic_res().is_diag_item(cx, sym::NonNull) |
| 58 | + // Parse `Box::into_raw` |
| 59 | + && let ExprKind::Call(box_into_raw, [arg]) = arg.kind |
| 60 | + && let ExprKind::Path(QPath::TypeRelative(r#box, into_raw)) = box_into_raw.kind |
| 61 | + && into_raw.ident.name == sym::into_raw |
| 62 | + && r#box.basic_res().is_lang_item(cx, LangItem::OwnedBox) |
| 63 | + && self.msrv.meets(cx, msrvs::NONNULL_FROM_MUT) |
| 64 | + { |
| 65 | + let ctxt = expr.span.ctxt(); |
| 66 | + let span = match cx.tcx.parent_hir_node(expr.hir_id) { |
| 67 | + Node::Block( |
| 68 | + block @ &Block { |
| 69 | + rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided), |
| 70 | + span: unsafe_span, |
| 71 | + .. |
| 72 | + }, |
| 73 | + ) if unsafe_span.ctxt() == ctxt && !is_expr_unsafe(cx, arg) && block.stmts.is_empty() => unsafe_span, |
| 74 | + _ => expr.span, |
| 75 | + }; |
| 76 | + |
| 77 | + let mut app = Applicability::MachineApplicable; |
| 78 | + let arg_name = snippet_with_context(cx, arg.span, ctxt, "_", &mut app).0; |
| 79 | + |
| 80 | + span_lint_and_sugg( |
| 81 | + cx, |
| 82 | + NONNULL_UNCHECKED_ON_BOX_PTR, |
| 83 | + span, |
| 84 | + "use of `NonNull::new_unchecked` with `Box::into_raw`", |
| 85 | + "try", |
| 86 | + format!("NonNull::from_mut(Box::leak({arg_name}))"), |
| 87 | + app, |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments