Skip to content

Commit 398ffc0

Browse files
committed
Taking a raw ref of a deref is always safe
1 parent 202008a commit 398ffc0

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ fn walk_unsafe(
9999
}
100100
Expr::Path(path) => mark_unsafe_path(path, current.into()),
101101
Expr::Ref { expr, rawness: Rawness::RawPtr, mutability: _ } => {
102-
if let Expr::Path(_) = body.exprs[*expr] {
103-
// Do not report unsafe for `addr_of[_mut]!(EXTERN_OR_MUT_STATIC)`,
104-
// see https://github.com/rust-lang/rust/pull/125834.
105-
return;
102+
match self.body.exprs[*expr] {
103+
Expr::Path(_) => return,
104+
// https://github.com/rust-lang/rust/pull/129248
105+
// Taking a raw ref to a deref place expr is always safe.
106+
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
107+
self.body
108+
.walk_child_exprs_without_pats(expr, |child| self.walk_expr(child));
109+
110+
return;
111+
}
112+
_ => (),
106113
}
107114
}
108115
Expr::MethodCall { .. } => {

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs

+15
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,19 @@ fn main() {
631631
"#,
632632
);
633633
}
634+
635+
#[test]
636+
fn raw_ref_reborrow_is_safe() {
637+
check_diagnostics(
638+
r#"
639+
fn main() {
640+
let ptr: *mut i32;
641+
let _addr = &raw const *ptr;
642+
let local = 1;
643+
let ptr = &local as *const i32;
644+
let _addr = &raw const *ptr;
645+
}
646+
"#,
647+
)
648+
}
634649
}

0 commit comments

Comments
 (0)