Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22381 from SomeoneToIgnore/unsafe-signature
Show `unsafe` in the signature help if applicable
Chayim Refael Friedman 3 weeks ago
parent 2c88285 · parent 4cc8ddf · commit ace4702
-rw-r--r--crates/hir/src/lib.rs7
-rw-r--r--crates/ide/src/signature_help.rs57
2 files changed, 64 insertions, 0 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index df7743fdba..9f94243062 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -2760,6 +2760,13 @@ impl Function {
}
}
+ pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
+ match self.id {
+ AnyFunctionId::FunctionId(id) => FunctionSignature::of(db, id).is_unsafe(),
+ AnyFunctionId::BuiltinDeriveImplMethod { .. } => false,
+ }
+ }
+
pub fn is_varargs(self, db: &dyn HirDatabase) -> bool {
match self.id {
AnyFunctionId::FunctionId(id) => FunctionSignature::of(db, id).is_varargs(),
diff --git a/crates/ide/src/signature_help.rs b/crates/ide/src/signature_help.rs
index dbecbafdd7..7854a14187 100644
--- a/crates/ide/src/signature_help.rs
+++ b/crates/ide/src/signature_help.rs
@@ -180,6 +180,9 @@ fn signature_help_for_call(
if func.is_async(db) {
format_to!(res.signature, "async ");
}
+ if func.is_unsafe(db) {
+ format_to!(res.signature, "unsafe ");
+ }
format_to!(res.signature, "fn {}", func.name(db).display(db, edition));
let generic_params = GenericDef::Function(func)
@@ -2685,4 +2688,58 @@ fn main() {
"#]],
);
}
+
+ #[test]
+ fn test_unsafe_function() {
+ check(
+ r#"
+//- minicore: sized, fn
+pub unsafe fn foo(x: u32, y: u32) -> u32 { x + y }
+
+fn main() {
+ unsafe { foo($0) }
+}
+ "#,
+ expect![[r#"
+ unsafe fn foo(x: u32, y: u32) -> u32
+ ^^^^^^ ------
+ "#]],
+ );
+ }
+
+ #[test]
+ fn test_const_unsafe_function() {
+ check(
+ r#"
+//- minicore: sized, fn
+pub const unsafe fn foo(x: u32, y: u32) -> u32 { x + y }
+
+fn main() {
+ unsafe { foo($0) }
+}
+ "#,
+ expect![[r#"
+ const unsafe fn foo(x: u32, y: u32) -> u32
+ ^^^^^^ ------
+ "#]],
+ );
+ }
+
+ #[test]
+ fn test_async_unsafe_function() {
+ check(
+ r#"
+//- minicore: sized, fn, future
+pub async unsafe fn foo(x: u32, y: u32) -> u32 { x + y }
+
+fn main() {
+ unsafe { foo($0) }
+}
+ "#,
+ expect![[r#"
+ async unsafe fn foo(x: u32, y: u32) -> u32
+ ^^^^^^ ------
+ "#]],
+ );
+ }
}