rustc_lint/
utils.rs
1use rustc_hir::{Expr, ExprKind};
2use rustc_span::sym;
3
4use crate::LateContext;
5
6pub(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 e = if let ExprKind::Cast(expr, _) = e.kind {
21 expr
22 } 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 } 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}