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