1use rustc_attr_ir::find_attr;
2use rustc_hir::{Expr, ExprKind};
3use rustc_span::sym;
4
5use crate::LateContext;
6
7pub(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 e = if let ExprKind::Cast(expr, _) = e.kind {
19 expr
20 } 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 } 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
52pub 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}