Skip to main content

rustc_lint/
utils.rs

1use rustc_attr_ir::find_attr;
2use rustc_hir::{Expr, ExprKind};
3use rustc_span::sym;
4
5use crate::LateContext;
6
7/// Given an expression, peel all of casts (`<expr> as ...`, `<expr>.cast{,_mut,_const}()`,
8/// `ptr::from_ref(<expr>)`, ...) and init expressions.
9///
10/// Returns the innermost expression.
11pub(crate) fn peel_casts<'tcx>(
12    cx: &LateContext<'tcx>,
13    mut e: &'tcx Expr<'tcx>,
14) -> &'tcx Expr<'tcx> {
15    loop {
16        e = e.peel_blocks();
17        // <expr> as ...
18        e = if let ExprKind::Cast(expr, _) = e.kind {
19            expr
20        // <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const()
21        } else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
22            && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
23            && #[allow(non_exhaustive_omitted_patterns)] match cx.tcx.get_diagnostic_name(def_id)
    {
    Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut |
        sym::ptr_cast_const) => true,
    _ => false,
}matches!(
24                cx.tcx.get_diagnostic_name(def_id),
25                Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const)
26            )
27        {
28            expr
29        // ptr::from_ref(<expr>), UnsafeCell::raw_get(<expr>) or mem::transmute<_, _>(<expr>)
30        } else if let ExprKind::Call(path, [arg]) = e.kind
31            && let ExprKind::Path(ref qpath) = path.kind
32            && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
33            && #[allow(non_exhaustive_omitted_patterns)] match cx.tcx.get_diagnostic_name(def_id)
    {
    Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get | sym::transmute) =>
        true,
    _ => false,
}matches!(
34                cx.tcx.get_diagnostic_name(def_id),
35                Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get | sym::transmute)
36            )
37        {
38            arg
39        } else {
40            let init = cx.expr_or_init(e);
41            if init.hir_id != e.hir_id {
42                init
43            } else {
44                break;
45            }
46        };
47    }
48
49    e
50}
51
52/// Crate to use for rustfix / diagnostic paths that exist in both `std` and `core`. Returns `None` for `#![no_core]`.
53pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {
54    if {
        'done:
            {
            for i in cx.tcx.hir_krate_attrs() {
                #[allow(unused_imports)]
                use ::rustc_attr_ir::AttributeKind::*;
                let i: &::rustc_attr_ir::Attribute = i;
                match i {
                    ::rustc_attr_ir::Attribute::Parsed(NoCore) => {
                        break 'done Some(());
                    }
                    ::rustc_attr_ir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }.is_some()find_attr!(cx.tcx, crate, NoCore) {
55        None
56    } else if {
        'done:
            {
            for i in cx.tcx.hir_krate_attrs() {
                #[allow(unused_imports)]
                use ::rustc_attr_ir::AttributeKind::*;
                let i: &::rustc_attr_ir::Attribute = i;
                match i {
                    ::rustc_attr_ir::Attribute::Parsed(NoStd) => {
                        break 'done Some(());
                    }
                    ::rustc_attr_ir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }.is_some()find_attr!(cx.tcx, crate, NoStd) {
57        Some("core")
58    } else {
59        Some("std")
60    }
61}