Skip to content

fix: Report calling unsafe fn pointer as unsafe #19051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading