]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/casts/cast_ptr_alignment.rs
Rollup merge of #105405 - sunfishcode:sunfishcode/export-dynamic, r=TaKO8Ki
[rust.git] / src / tools / clippy / clippy_lints / src / casts / cast_ptr_alignment.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::ty::is_c_void;
3 use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, match_any_def_paths, paths};
4 use rustc_hir::{Expr, ExprKind, GenericArg};
5 use rustc_lint::LateContext;
6 use rustc_middle::ty::layout::LayoutOf;
7 use rustc_middle::ty::{self, Ty};
8
9 use super::CAST_PTR_ALIGNMENT;
10
11 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
12     if let ExprKind::Cast(cast_expr, cast_to) = expr.kind {
13         if is_hir_ty_cfg_dependant(cx, cast_to) {
14             return;
15         }
16         let (cast_from, cast_to) = (
17             cx.typeck_results().expr_ty(cast_expr),
18             cx.typeck_results().expr_ty(expr),
19         );
20         lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
21     } else if let ExprKind::MethodCall(method_path, self_arg, ..) = &expr.kind {
22         if method_path.ident.name == sym!(cast)
23             && let Some(generic_args) = method_path.args
24             && let [GenericArg::Type(cast_to)] = generic_args.args
25             // There probably is no obvious reason to do this, just to be consistent with `as` cases.
26             && !is_hir_ty_cfg_dependant(cx, cast_to)
27         {
28             let (cast_from, cast_to) =
29                 (cx.typeck_results().expr_ty(self_arg), cx.typeck_results().expr_ty(expr));
30             lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
31         }
32     }
33 }
34
35 fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {
36     if let ty::RawPtr(from_ptr_ty) = &cast_from.kind()
37         && let ty::RawPtr(to_ptr_ty) = &cast_to.kind()
38         && let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty)
39         && let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty)
40         && from_layout.align.abi < to_layout.align.abi
41         // with c_void, we inherently need to trust the user
42         && !is_c_void(cx, from_ptr_ty.ty)
43         // when casting from a ZST, we don't know enough to properly lint
44         && !from_layout.is_zst()
45         && !is_used_as_unaligned(cx, expr)
46     {
47         span_lint(
48             cx,
49             CAST_PTR_ALIGNMENT,
50             expr.span,
51             &format!(
52                 "casting from `{cast_from}` to a more-strictly-aligned pointer (`{cast_to}`) ({} < {} bytes)",
53                 from_layout.align.abi.bytes(),
54                 to_layout.align.abi.bytes(),
55             ),
56         );
57     }
58 }
59
60 fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
61     let Some(parent) = get_parent_expr(cx, e) else {
62         return false;
63     };
64     match parent.kind {
65         ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
66             if matches!(name.ident.as_str(), "read_unaligned" | "write_unaligned")
67                 && let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
68                 && let Some(def_id) = cx.tcx.impl_of_method(def_id)
69                 && cx.tcx.type_of(def_id).is_unsafe_ptr()
70             {
71                 true
72             } else {
73                 false
74             }
75         },
76         ExprKind::Call(func, [arg, ..]) if arg.hir_id == e.hir_id => {
77             static PATHS: &[&[&str]] = &[
78                 paths::PTR_READ_UNALIGNED.as_slice(),
79                 paths::PTR_WRITE_UNALIGNED.as_slice(),
80                 paths::PTR_UNALIGNED_VOLATILE_LOAD.as_slice(),
81                 paths::PTR_UNALIGNED_VOLATILE_STORE.as_slice(),
82             ];
83             if let ExprKind::Path(path) = &func.kind
84                 && let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
85                 && match_any_def_paths(cx, def_id, PATHS).is_some()
86             {
87                 true
88             } else {
89                 false
90             }
91         },
92         _ => false,
93     }
94 }