Taking a raw ref of a deref is always safe

This commit is contained in:
Lukas Wirth 2024-12-18 13:35:51 +01:00
parent 202008a1b8
commit 840f6588cc
2 changed files with 27 additions and 4 deletions

View file

@ -99,10 +99,18 @@ fn walk_unsafe(
}
Expr::Path(path) => mark_unsafe_path(path, current.into()),
Expr::Ref { expr, rawness: Rawness::RawPtr, mutability: _ } => {
if let Expr::Path(_) = body.exprs[*expr] {
// Do not report unsafe for `addr_of[_mut]!(EXTERN_OR_MUT_STATIC)`,
// see https://github.com/rust-lang/rust/pull/125834.
return;
match body.exprs[*expr] {
Expr::Path(_) => return,
// https://github.com/rust-lang/rust/pull/129248
// Taking a raw ref to a deref place expr is always safe.
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
body.walk_child_exprs(expr, |child| {
walk_unsafe(db, infer, body, resolver, def, child, inside_unsafe_block, unsafe_expr_cb);
});
return;
}
_ => (),
}
}
Expr::MethodCall { .. } => {

View file

@ -631,4 +631,19 @@ fn main() {
"#,
);
}
#[test]
fn raw_ref_reborrow_is_safe() {
check_diagnostics(
r#"
fn main() {
let ptr: *mut i32;
let _addr = &raw const *ptr;
let local = 1;
let ptr = &local as *const i32;
let _addr = &raw const *ptr;
}
"#,
)
}
}