rustc_lint/
utils.rs

1use rustc_hir::{Expr, ExprKind};
2use rustc_span::sym;
3
4use crate::LateContext;
5
6/// Given an expression, peel all of casts (`<expr> as ...`, `<expr>.cast{,_mut,_const}()`,
7/// `ptr::from_ref(<expr>)`, ...) and init expressions.
8///
9/// Returns the innermost expression and a boolean representing if one of the casts was
10/// `UnsafeCell::raw_get(<expr>)`
11pub(crate) fn peel_casts<'tcx>(
12    cx: &LateContext<'tcx>,
13    mut e: &'tcx Expr<'tcx>,
14) -> (&'tcx Expr<'tcx>, bool) {
15    let mut gone_trough_unsafe_cell_raw_get = false;
16
17    loop {
18        e = e.peel_blocks();
19        // <expr> as ...
20        e = if let ExprKind::Cast(expr, _) = e.kind {
21            expr
22        // <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const()
23        } else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
24            && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
25            && matches!(
26                cx.tcx.get_diagnostic_name(def_id),
27                Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const)
28            )
29        {
30            expr
31        // ptr::from_ref(<expr>), UnsafeCell::raw_get(<expr>) or mem::transmute<_, _>(<expr>)
32        } else if let ExprKind::Call(path, [arg]) = e.kind
33            && let ExprKind::Path(ref qpath) = path.kind
34            && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
35            && matches!(
36                cx.tcx.get_diagnostic_name(def_id),
37                Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get | sym::transmute)
38            )
39        {
40            if cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) {
41                gone_trough_unsafe_cell_raw_get = true;
42            }
43            arg
44        } else {
45            let init = cx.expr_or_init(e);
46            if init.hir_id != e.hir_id {
47                init
48            } else {
49                break;
50            }
51        };
52    }
53
54    (e, gone_trough_unsafe_cell_raw_get)
55}