Skip to content

Commit

Permalink
Merge pull request #19051 from ChayimFriedman2/fn-ptr-unsafe
Browse files Browse the repository at this point in the history
fix: Report calling unsafe fn pointer as unsafe
  • Loading branch information
Veykril authored Jan 27, 2025
2 parents b27c5b4 + 9c27e02 commit 7c387ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/hir-ty/src/diagnostics/unsafe_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
12 changes: 12 additions & 0 deletions crates/ide-diagnostics/src/handlers/missing_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,18 @@ fn bar() {
fn baz() {
foo();
// ^^^^^ 💡 error: call to unsafe function is unsafe and requires an unsafe function or block
}
"#,
);
}

#[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
}
"#,
);
Expand Down

0 comments on commit 7c387ed

Please sign in to comment.