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