Skip to content

Commit cadc468

Browse files
authored
Merge pull request rust-lang#19051 from ChayimFriedman2/fn-ptr-unsafe
fix: Report calling unsafe fn pointer as unsafe
2 parents da33e7c + 1ba8943 commit cadc468

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,15 @@ impl<'a> UnsafeVisitor<'a> {
240240
let inside_assignment = mem::replace(&mut self.inside_assignment, false);
241241
match expr {
242242
&Expr::Call { callee, .. } => {
243-
if let Some(func) = self.infer[callee].as_fn_def(self.db) {
243+
let callee = &self.infer[callee];
244+
if let Some(func) = callee.as_fn_def(self.db) {
244245
self.check_call(current, func);
245246
}
247+
if let TyKind::Function(fn_ptr) = callee.kind(Interner) {
248+
if fn_ptr.sig.safety == chalk_ir::Safety::Unsafe {
249+
self.on_unsafe_op(current.into(), UnsafetyReason::UnsafeFnCall);
250+
}
251+
}
246252
}
247253
Expr::Path(path) => {
248254
let guard =

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

+12
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,18 @@ fn bar() {
862862
fn baz() {
863863
foo();
864864
// ^^^^^ 💡 error: call to unsafe function is unsafe and requires an unsafe function or block
865+
}
866+
"#,
867+
);
868+
}
869+
870+
#[test]
871+
fn unsafe_fn_ptr_call() {
872+
check_diagnostics(
873+
r#"
874+
fn f(it: unsafe fn()){
875+
it();
876+
// ^^^^ 💡 error: call to unsafe function is unsafe and requires an unsafe function or block
865877
}
866878
"#,
867879
);

0 commit comments

Comments
 (0)