diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs b/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs index c7f7fb7ad3d35..d00b9f3e445f1 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs @@ -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 { .. } => { diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs index a630d3c7c36d1..796531ac6c509 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs @@ -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; +} +"#, + ) + } }