Taking a raw ref of a deref is always safe

This commit is contained in:
Lukas Wirth 2024-12-18 11:32:29 +01:00
parent 2780dfd80c
commit dfa46292b6
2 changed files with 27 additions and 2 deletions

View file

@ -193,10 +193,19 @@ impl<'a> UnsafeVisitor<'a> {
self.resolver.reset_to_guard(guard);
}
Expr::Ref { expr, rawness: Rawness::RawPtr, mutability: _ } => {
if let Expr::Path(_) = self.body.exprs[*expr] {
match self.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;
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 } => {
self.body
.walk_child_exprs_without_pats(expr, |child| self.walk_expr(child));
return;
}
_ => (),
}
}
Expr::MethodCall { .. } => {

View file

@ -778,4 +778,20 @@ fn bar(mut v: Union2) {
"#,
)
}
#[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;
}
"#,
)
}
}