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