Skip to content

Commit 477b987

Browse files
committed
Observe unsafeness when generating manual impls of former derives
1 parent 0c1b483 commit 477b987

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,4 +2363,33 @@ impl other_file_2::Trait for MyStruct {
23632363
}"#,
23642364
);
23652365
}
2366+
2367+
#[test]
2368+
fn unsafeness_observed() {
2369+
check_assist(
2370+
add_missing_impl_members,
2371+
r#"
2372+
unsafe trait UnsafeTrait {
2373+
unsafe fn unsafe_fn();
2374+
}
2375+
2376+
struct A {}
2377+
2378+
impl Uns$0afeTrait for A {}
2379+
"#,
2380+
r#"
2381+
unsafe trait UnsafeTrait {
2382+
unsafe fn unsafe_fn();
2383+
}
2384+
2385+
struct A {}
2386+
2387+
unsafe impl UnsafeTrait for A {
2388+
unsafe fn unsafe_fn() {
2389+
${0:todo!()}
2390+
}
2391+
}
2392+
"#,
2393+
);
2394+
}
23662395
}

crates/ide-assists/src/utils.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::{
2424
make,
2525
syntax_factory::SyntaxFactory,
2626
},
27-
ted,
27+
ted::{self, Position},
2828
};
2929

3030
use crate::assist_context::{AssistContext, SourceChangeBuilder};
@@ -212,6 +212,10 @@ pub fn add_trait_assoc_items_to_impl(
212212
});
213213

214214
let assoc_item_list = impl_.get_or_create_assoc_item_list();
215+
if trait_.is_unsafe(sema.db) {
216+
ted::insert(Position::first_child_of(impl_.syntax()), make::token(T![unsafe]));
217+
}
218+
215219
let mut first_item = None;
216220
for item in items {
217221
first_item.get_or_insert_with(|| item.clone());

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ pub mod tokens {
12761276

12771277
pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| {
12781278
SourceFile::parse(
1279-
"use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nimpl A for B where: {}",
1279+
"use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nunsafe impl A for B where: {}",
12801280
Edition::CURRENT,
12811281
)
12821282
});

0 commit comments

Comments
 (0)