Unnamed repository; edit this file 'description' to name the repository.
Report calling unsafe fn pointer as unsafe
Chayim Refael Friedman 2025-01-27
parent 606cfef · commit 9c27e02
-rw-r--r--crates/hir-ty/src/diagnostics/unsafe_check.rs8
-rw-r--r--crates/ide-diagnostics/src/handlers/missing_unsafe.rs12
2 files changed, 19 insertions, 1 deletions
diff --git a/crates/hir-ty/src/diagnostics/unsafe_check.rs b/crates/hir-ty/src/diagnostics/unsafe_check.rs
index f655692429..2aca99c93b 100644
--- a/crates/hir-ty/src/diagnostics/unsafe_check.rs
+++ b/crates/hir-ty/src/diagnostics/unsafe_check.rs
@@ -240,9 +240,15 @@ impl<'a> UnsafeVisitor<'a> {
let inside_assignment = mem::replace(&mut self.inside_assignment, false);
match expr {
&Expr::Call { callee, .. } => {
- if let Some(func) = self.infer[callee].as_fn_def(self.db) {
+ let callee = &self.infer[callee];
+ if let Some(func) = callee.as_fn_def(self.db) {
self.check_call(current, func);
}
+ if let TyKind::Function(fn_ptr) = callee.kind(Interner) {
+ if fn_ptr.sig.safety == chalk_ir::Safety::Unsafe {
+ self.on_unsafe_op(current.into(), UnsafetyReason::UnsafeFnCall);
+ }
+ }
}
Expr::Path(path) => {
let guard =
diff --git a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
index d48464769c..323a5723d4 100644
--- a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
+++ b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
@@ -866,4 +866,16 @@ fn baz() {
"#,
);
}
+
+ #[test]
+ fn unsafe_fn_ptr_call() {
+ check_diagnostics(
+ r#"
+fn f(it: unsafe fn()){
+ it();
+ // ^^^^ 💡 error: call to unsafe function is unsafe and requires an unsafe function or block
+}
+ "#,
+ );
+ }
}