|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::SpanRangeExt; |
| 3 | +use clippy_utils::ty::is_normalizable; |
| 4 | +use clippy_utils::{expr_or_init, match_def_path, path_def_id, paths, std_or_core}; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, Ty, TyKind}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_span::source_map::Spanned; |
| 10 | + |
| 11 | +use super::MANUAL_DANGLING_PTR; |
| 12 | + |
| 13 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) { |
| 14 | + if let TyKind::Ptr(ref ptr_ty) = to.kind { |
| 15 | + let init_expr = expr_or_init(cx, from); |
| 16 | + if is_expr_const_aligned(cx, init_expr, ptr_ty.ty) |
| 17 | + && let Some(std_or_core) = std_or_core(cx) |
| 18 | + { |
| 19 | + let sugg_fn = match ptr_ty.mutbl { |
| 20 | + Mutability::Not => "ptr::dangling", |
| 21 | + Mutability::Mut => "ptr::dangling_mut", |
| 22 | + }; |
| 23 | + |
| 24 | + let sugg = if let TyKind::Infer(()) = ptr_ty.ty.kind { |
| 25 | + format!("{std_or_core}::{sugg_fn}()") |
| 26 | + } else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_source_text(cx) { |
| 27 | + format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()") |
| 28 | + } else { |
| 29 | + return; |
| 30 | + }; |
| 31 | + |
| 32 | + span_lint_and_sugg( |
| 33 | + cx, |
| 34 | + MANUAL_DANGLING_PTR, |
| 35 | + expr.span, |
| 36 | + "manual creation of a dangling pointer", |
| 37 | + "use", |
| 38 | + sugg, |
| 39 | + Applicability::MachineApplicable, |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Checks if the given expression is a call to `align_of` whose generic argument matches the target |
| 46 | +// type, or a positive constant literal that matches the target type's alignment. |
| 47 | +fn is_expr_const_aligned(cx: &LateContext<'_>, expr: &Expr<'_>, to: &Ty<'_>) -> bool { |
| 48 | + match expr.kind { |
| 49 | + ExprKind::Call(fun, _) => is_align_of_call(cx, fun, to), |
| 50 | + ExprKind::Lit(lit) => is_literal_aligned(cx, lit, to), |
| 51 | + _ => false, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn is_align_of_call(cx: &LateContext<'_>, fun: &Expr<'_>, to: &Ty<'_>) -> bool { |
| 56 | + if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind |
| 57 | + && let Some(fun_id) = path_def_id(cx, fun) |
| 58 | + && match_def_path(cx, fun_id, &paths::ALIGN_OF) |
| 59 | + && let Some(args) = path.segments.last().and_then(|seg| seg.args) |
| 60 | + && let [GenericArg::Type(generic_ty)] = args.args |
| 61 | + { |
| 62 | + let typeck = cx.typeck_results(); |
| 63 | + return typeck.node_type(generic_ty.hir_id) == typeck.node_type(to.hir_id); |
| 64 | + } |
| 65 | + false |
| 66 | +} |
| 67 | + |
| 68 | +fn is_literal_aligned(cx: &LateContext<'_>, lit: &Spanned<LitKind>, to: &Ty<'_>) -> bool { |
| 69 | + let LitKind::Int(val, _) = lit.node else { return false }; |
| 70 | + if val == 0 { |
| 71 | + return false; |
| 72 | + } |
| 73 | + let to_mid_ty = cx.typeck_results().node_type(to.hir_id); |
| 74 | + is_normalizable(cx, cx.param_env, to_mid_ty) |
| 75 | + && cx |
| 76 | + .tcx |
| 77 | + .layout_of(cx.typing_env().as_query_input(to_mid_ty)) |
| 78 | + .is_ok_and(|layout| { |
| 79 | + let align = u128::from(layout.align.abi.bytes()); |
| 80 | + u128::from(val) <= align |
| 81 | + }) |
| 82 | +} |
0 commit comments