]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/transmute/utils.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / transmute / utils.rs
1 use clippy_utils::last_path_segment;
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::is_normalizable;
4 use if_chain::if_chain;
5 use rustc_hir::{Expr, GenericArg, QPath, TyKind};
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, cast::CastKind, Ty};
8 use rustc_span::DUMMY_SP;
9 use rustc_typeck::check::{cast::CastCheck, FnCtxt, Inherited};
10
11 /// Gets the snippet of `Bar` in `…::transmute<Foo, &Bar>`. If that snippet is
12 /// not available , use
13 /// the type's `ToString` implementation. In weird cases it could lead to types
14 /// with invalid `'_`
15 /// lifetime, but it should be rare.
16 pub(super) fn get_type_snippet(cx: &LateContext<'_>, path: &QPath<'_>, to_ref_ty: Ty<'_>) -> String {
17 let seg = last_path_segment(path);
18 if_chain! {
19 if let Some(params) = seg.args;
20 if !params.parenthesized;
21 if let Some(to_ty) = params.args.iter().filter_map(|arg| match arg {
22 GenericArg::Type(ty) => Some(ty),
23 _ => None,
24 }).nth(1);
25 if let TyKind::Rptr(_, ref to_ty) = to_ty.kind;
26 then {
27 return snippet(cx, to_ty.ty.span, &to_ref_ty.to_string()).to_string();
28 }
29 }
30
31 to_ref_ty.to_string()
32 }
33
34 // check if the component types of the transmuted collection and the result have different ABI,
35 // size or alignment
36 pub(super) fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
37 let empty_param_env = ty::ParamEnv::empty();
38 // check if `from` and `to` are normalizable to avoid ICE (#4968)
39 if !(is_normalizable(cx, empty_param_env, from) && is_normalizable(cx, empty_param_env, to)) {
40 return false;
41 }
42 let from_ty_layout = cx.tcx.layout_of(empty_param_env.and(from));
43 let to_ty_layout = cx.tcx.layout_of(empty_param_env.and(to));
44 if let (Ok(from_layout), Ok(to_layout)) = (from_ty_layout, to_ty_layout) {
45 from_layout.size != to_layout.size || from_layout.align != to_layout.align || from_layout.abi != to_layout.abi
46 } else {
47 // no idea about layout, so don't lint
48 false
49 }
50 }
51
52 /// Check if the type conversion can be expressed as a pointer cast, instead of
53 /// a transmute. In certain cases, including some invalid casts from array
54 /// references to pointers, this may cause additional errors to be emitted and/or
55 /// ICE error messages. This function will panic if that occurs.
56 pub(super) fn can_be_expressed_as_pointer_cast<'tcx>(
57 cx: &LateContext<'tcx>,
58 e: &'tcx Expr<'_>,
59 from_ty: Ty<'tcx>,
60 to_ty: Ty<'tcx>,
61 ) -> bool {
62 use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
63 matches!(
64 check_cast(cx, e, from_ty, to_ty),
65 Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast)
66 )
67 }
68
69 /// If a cast from `from_ty` to `to_ty` is valid, returns an Ok containing the kind of
70 /// the cast. In certain cases, including some invalid casts from array references
71 /// to pointers, this may cause additional errors to be emitted and/or ICE error
72 /// messages. This function will panic if that occurs.
73 fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> Option<CastKind> {
74 let hir_id = e.hir_id;
75 let local_def_id = hir_id.owner;
76
77 Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
78 let fn_ctxt = FnCtxt::new(&inherited, cx.param_env, hir_id);
79
80 // If we already have errors, we can't be sure we can pointer cast.
81 assert!(
82 !fn_ctxt.errors_reported_since_creation(),
83 "Newly created FnCtxt contained errors"
84 );
85
86 if let Ok(check) = CastCheck::new(
87 &fn_ctxt, e, from_ty, to_ty,
88 // We won't show any error to the user, so we don't care what the span is here.
89 DUMMY_SP, DUMMY_SP,
90 ) {
91 let res = check.do_check(&fn_ctxt);
92
93 // do_check's documentation says that it might return Ok and create
94 // errors in the fcx instead of returing Err in some cases. Those cases
95 // should be filtered out before getting here.
96 assert!(
97 !fn_ctxt.errors_reported_since_creation(),
98 "`fn_ctxt` contained errors after cast check!"
99 );
100
101 res.ok()
102 } else {
103 None
104 }
105 })
106 }