From 64a34bc947a39dd7200b80bf38c7d43b1f0bab31 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Mon, 17 Nov 2025 17:39:10 +0800 Subject: [PATCH] =?UTF-8?q?Add=20`unsafe(=E2=80=A6)`=20attribute=20complet?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Like `unsafe(no_mangle)` --- .../src/completions/attribute.rs | 3 +- crates/ide-completion/src/tests/attribute.rs | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs index 297ce3339e02..20776f6c49f6 100644 --- a/crates/ide-completion/src/completions/attribute.rs +++ b/crates/ide-completion/src/completions/attribute.rs @@ -231,7 +231,7 @@ const fn attr( macro_rules! attrs { // attributes applicable to all items [@ { item $($tt:tt)* } {$($acc:tt)*}] => { - attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "docinclude", "must_use", "no_mangle" }) + attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "docinclude", "must_use", "no_mangle", "unsafe" }) }; // attributes applicable to all adts [@ { adt $($tt:tt)* } {$($acc:tt)*}] => { @@ -395,6 +395,7 @@ const ATTRIBUTES: &[AttrCompletion] = &[ attr("track_caller", None, None), attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}")) .prefer_inner(), + attr("unsafe(…)", Some("unsafe"), Some("unsafe($0)")), attr("used", None, None), attr("warn(…)", Some("warn"), Some("warn(${0:lint})")), attr( diff --git a/crates/ide-completion/src/tests/attribute.rs b/crates/ide-completion/src/tests/attribute.rs index 2a6238997b8e..3538dac2bba2 100644 --- a/crates/ide-completion/src/tests/attribute.rs +++ b/crates/ide-completion/src/tests/attribute.rs @@ -61,6 +61,7 @@ pub struct Foo(#[m$0] i32); at target_feature(enable = "…") at test at track_caller + at unsafe(…) at used at warn(…) md mac @@ -95,6 +96,7 @@ struct Foo; at no_mangle at non_exhaustive at repr(…) + at unsafe(…) at warn(…) md proc_macros kw crate:: @@ -173,6 +175,7 @@ fn attr_on_source_file() { at no_std at recursion_limit = "…" at type_length_limit = … + at unsafe(…) at warn(…) at windows_subsystem = "…" kw crate:: @@ -201,6 +204,7 @@ fn attr_on_module() { at must_use at no_mangle at path = "…" + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -224,6 +228,7 @@ fn attr_on_module() { at must_use at no_implicit_prelude at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -252,6 +257,7 @@ fn attr_on_macro_rules() { at macro_use at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -277,6 +283,7 @@ fn attr_on_macro_def() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -303,6 +310,7 @@ fn attr_on_extern_crate() { at macro_use at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -328,6 +336,7 @@ fn attr_on_use() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -353,6 +362,7 @@ fn attr_on_type_alias() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -387,6 +397,7 @@ struct Foo; at no_mangle at non_exhaustive at repr(…) + at unsafe(…) at warn(…) md core kw crate:: @@ -416,6 +427,7 @@ fn attr_on_enum() { at no_mangle at non_exhaustive at repr(…) + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -441,6 +453,7 @@ fn attr_on_const() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -470,6 +483,7 @@ fn attr_on_static() { at link_section = "…" at must_use at no_mangle + at unsafe(…) at used at warn(…) kw crate:: @@ -497,6 +511,7 @@ fn attr_on_trait() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -524,6 +539,7 @@ fn attr_on_impl() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -545,6 +561,7 @@ fn attr_on_impl() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -572,6 +589,7 @@ fn attr_with_qualifier() { at forbid(…) at must_use at no_mangle + at unsafe(…) at warn(…) "#]], ); @@ -592,11 +610,43 @@ fn attr_with_qualifier() { at must_use at no_mangle at on_unimplemented + at unsafe(…) at warn(…) "#]], ); } +#[test] +fn attr_on_unsafe_attr() { + check( + r#"#[unsafe($0)] static FOO: () = ()"#, + expect![[r#" + at allow(…) + at cfg(…) + at cfg_attr(…) + at deny(…) + at deprecated + at doc = "…" + at doc = include_str!("…") + at doc(alias = "…") + at doc(hidden) + at expect(…) + at export_name = "…" + at forbid(…) + at global_allocator + at link_name = "…" + at link_section = "…" + at must_use + at no_mangle + at unsafe(…) + at used + at warn(…) + kw crate:: + kw self:: + "#]], + ); +} + #[test] fn attr_diagnostic_on_unimplemented() { check( @@ -643,6 +693,7 @@ fn attr_on_extern_block() { at link at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -665,6 +716,7 @@ fn attr_on_extern_block() { at link at must_use at no_mangle + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -723,6 +775,7 @@ fn attr_on_fn() { at target_feature(enable = "…") at test at track_caller + at unsafe(…) at warn(…) kw crate:: kw self:: @@ -773,6 +826,7 @@ fn attr_in_source_file_end() { at target_feature(enable = "…") at test at track_caller + at unsafe(…) at used at warn(…) kw crate::