Skip to content

Commit c70b07b

Browse files
authored
Merge pull request #18711 from Veykril/push-kwurwxttmqwo
Taking a raw ref of a deref is always safe
2 parents cbf3443 + dfa4629 commit c70b07b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

crates/hir-ty/src/diagnostics/unsafe_check.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,19 @@ impl<'a> UnsafeVisitor<'a> {
193193
self.resolver.reset_to_guard(guard);
194194
}
195195
Expr::Ref { expr, rawness: Rawness::RawPtr, mutability: _ } => {
196-
if let Expr::Path(_) = self.body.exprs[*expr] {
196+
match self.body.exprs[*expr] {
197197
// Do not report unsafe for `addr_of[_mut]!(EXTERN_OR_MUT_STATIC)`,
198198
// see https://github.com/rust-lang/rust/pull/125834.
199-
return;
199+
Expr::Path(_) => return,
200+
// https://github.com/rust-lang/rust/pull/129248
201+
// Taking a raw ref to a deref place expr is always safe.
202+
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
203+
self.body
204+
.walk_child_exprs_without_pats(expr, |child| self.walk_expr(child));
205+
206+
return;
207+
}
208+
_ => (),
200209
}
201210
}
202211
Expr::MethodCall { .. } => {

crates/ide-diagnostics/src/handlers/missing_unsafe.rs

+16
Original file line numberDiff line numberDiff line change
@@ -778,4 +778,20 @@ fn bar(mut v: Union2) {
778778
"#,
779779
)
780780
}
781+
782+
#[test]
783+
fn raw_ref_reborrow_is_safe() {
784+
check_diagnostics(
785+
r#"
786+
fn main() {
787+
let ptr: *mut i32;
788+
let _addr = &raw const *ptr;
789+
790+
let local = 1;
791+
let ptr = &local as *const i32;
792+
let _addr = &raw const *ptr;
793+
}
794+
"#,
795+
)
796+
}
781797
}

0 commit comments

Comments
 (0)