]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/transmute/utils.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / utils.rs
1 use rustc_hir::Expr;
2 use rustc_lint::LateContext;
3 use rustc_middle::ty::{cast::CastKind, Ty};
4 use rustc_span::DUMMY_SP;
5 use rustc_typeck::check::{cast::{self, CastCheckResult}, FnCtxt, Inherited};
6
7 // check if the component types of the transmuted collection and the result have different ABI,
8 // size or alignment
9 pub(super) fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
10 if let Ok(from) = cx.tcx.try_normalize_erasing_regions(cx.param_env, from)
11 && let Ok(to) = cx.tcx.try_normalize_erasing_regions(cx.param_env, to)
12 && let Ok(from_layout) = cx.tcx.layout_of(cx.param_env.and(from))
13 && let Ok(to_layout) = cx.tcx.layout_of(cx.param_env.and(to))
14 {
15 from_layout.size != to_layout.size || from_layout.align.abi != to_layout.align.abi
16 } else {
17 // no idea about layout, so don't lint
18 false
19 }
20 }
21
22 /// Check if the type conversion can be expressed as a pointer cast, instead of
23 /// a transmute. In certain cases, including some invalid casts from array
24 /// references to pointers, this may cause additional errors to be emitted and/or
25 /// ICE error messages. This function will panic if that occurs.
26 pub(super) fn can_be_expressed_as_pointer_cast<'tcx>(
27 cx: &LateContext<'tcx>,
28 e: &'tcx Expr<'_>,
29 from_ty: Ty<'tcx>,
30 to_ty: Ty<'tcx>,
31 ) -> bool {
32 use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
33 matches!(
34 check_cast(cx, e, from_ty, to_ty),
35 Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast)
36 )
37 }
38
39 /// If a cast from `from_ty` to `to_ty` is valid, returns an Ok containing the kind of
40 /// the cast. In certain cases, including some invalid casts from array references
41 /// to pointers, this may cause additional errors to be emitted and/or ICE error
42 /// messages. This function will panic if that occurs.
43 fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> Option<CastKind> {
44 let hir_id = e.hir_id;
45 let local_def_id = hir_id.owner;
46
47 Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
48 let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, hir_id);
49
50 // If we already have errors, we can't be sure we can pointer cast.
51 assert!(
52 !fn_ctxt.errors_reported_since_creation(),
53 "Newly created FnCtxt contained errors"
54 );
55
56 if let CastCheckResult::Deferred(check) = cast::check_cast(
57 &fn_ctxt, e, from_ty, to_ty,
58 // We won't show any error to the user, so we don't care what the span is here.
59 DUMMY_SP, DUMMY_SP,
60 ) {
61 let res = check.do_check(&fn_ctxt);
62
63 // do_check's documentation says that it might return Ok and create
64 // errors in the fcx instead of returning Err in some cases. Those cases
65 // should be filtered out before getting here.
66 assert!(
67 !fn_ctxt.errors_reported_since_creation(),
68 "`fn_ctxt` contained errors after cast check!"
69 );
70
71 res.ok()
72 } else {
73 None
74 }
75 })
76 }