Skip to content

Commit db576c1

Browse files
committed
Expose peel_casts method as an util method inside rustc_lint
1 parent 48994b1 commit db576c1

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

Diff for: compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod types;
8080
mod unit_bindings;
8181
mod unqualified_local_imports;
8282
mod unused;
83+
mod utils;
8384

8485
use async_closures::AsyncClosureUsage;
8586
use async_fn_in_trait::AsyncFnInTrait;

Diff for: compiler/rustc_lint/src/reference_casting.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_session::{declare_lint, declare_lint_pass};
66
use rustc_span::sym;
77

88
use crate::lints::InvalidReferenceCastingDiag;
9+
use crate::utils::peel_casts;
910
use crate::{LateContext, LateLintPass, LintContext};
1011

1112
declare_lint! {
@@ -235,46 +236,3 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
235236
None
236237
}
237238
}
238-
239-
fn peel_casts<'tcx>(cx: &LateContext<'tcx>, mut e: &'tcx Expr<'tcx>) -> (&'tcx Expr<'tcx>, bool) {
240-
let mut gone_trough_unsafe_cell_raw_get = false;
241-
242-
loop {
243-
e = e.peel_blocks();
244-
// <expr> as ...
245-
e = if let ExprKind::Cast(expr, _) = e.kind {
246-
expr
247-
// <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const()
248-
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
249-
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
250-
&& matches!(
251-
cx.tcx.get_diagnostic_name(def_id),
252-
Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const)
253-
)
254-
{
255-
expr
256-
// ptr::from_ref(<expr>), UnsafeCell::raw_get(<expr>) or mem::transmute<_, _>(<expr>)
257-
} else if let ExprKind::Call(path, [arg]) = e.kind
258-
&& let ExprKind::Path(ref qpath) = path.kind
259-
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
260-
&& matches!(
261-
cx.tcx.get_diagnostic_name(def_id),
262-
Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get | sym::transmute)
263-
)
264-
{
265-
if cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) {
266-
gone_trough_unsafe_cell_raw_get = true;
267-
}
268-
arg
269-
} else {
270-
let init = cx.expr_or_init(e);
271-
if init.hir_id != e.hir_id {
272-
init
273-
} else {
274-
break;
275-
}
276-
};
277-
}
278-
279-
(e, gone_trough_unsafe_cell_raw_get)
280-
}

Diff for: compiler/rustc_lint/src/utils.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use rustc_hir::{Expr, ExprKind};
2+
use rustc_span::sym;
3+
4+
use 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 outermost expression and a boolean representing if one of the casts was
10+
/// `UnsafeCell::raw_get(<expr>)`
11+
pub(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+
}

0 commit comments

Comments
 (0)