rapx/analysis/safedrop/
types.rs

1use super::corner_handle::is_corner_adt;
2use rustc_middle::ty;
3use rustc_middle::ty::{Ty, TyCtxt};
4
5#[derive(PartialEq, Debug, Copy, Clone)]
6pub enum TyKind {
7    Adt,
8    RawPtr,
9    Tuple,
10    CornerCase,
11    Ref,
12}
13
14pub fn kind<'tcx>(current_ty: Ty<'tcx>) -> TyKind {
15    match current_ty.kind() {
16        ty::RawPtr(..) => TyKind::RawPtr,
17        ty::Ref(..) => TyKind::Ref,
18        ty::Tuple(..) => TyKind::Tuple,
19        ty::Adt(ref adt_def, _) => {
20            if is_corner_adt(format!("{:?}", adt_def)) {
21                return TyKind::CornerCase;
22            } else {
23                return TyKind::Adt;
24            }
25        }
26        _ => TyKind::Adt,
27    }
28}
29
30pub fn is_not_drop<'tcx>(tcx: TyCtxt<'tcx>, current_ty: Ty<'tcx>) -> bool {
31    match current_ty.kind() {
32        ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => true,
33        ty::Array(ref tys, _) => is_not_drop(tcx, *tys),
34        ty::Adt(ref adtdef, ref substs) => {
35            for field in adtdef.all_fields() {
36                if !is_not_drop(tcx, field.ty(tcx, substs)) {
37                    return false;
38                }
39            }
40            true
41        }
42        ty::Tuple(ref tuple_fields) => {
43            for tys in tuple_fields.iter() {
44                if !is_not_drop(tcx, tys) {
45                    return false;
46                }
47            }
48            true
49        }
50        _ => false,
51    }
52}