Observe unsafeness only for replace_derive_with_manual_impl

This commit is contained in:
Ali Bektas 2025-03-09 18:47:40 +01:00
parent 477b987179
commit 824df43d2d
3 changed files with 6 additions and 34 deletions

View File

@ -2363,33 +2363,4 @@ impl other_file_2::Trait for MyStruct {
}"#,
);
}
#[test]
fn unsafeness_observed() {
check_assist(
add_missing_impl_members,
r#"
unsafe trait UnsafeTrait {
unsafe fn unsafe_fn();
}
struct A {}
impl Uns$0afeTrait for A {}
"#,
r#"
unsafe trait UnsafeTrait {
unsafe fn unsafe_fn();
}
struct A {}
unsafe impl UnsafeTrait for A {
unsafe fn unsafe_fn() {
${0:todo!()}
}
}
"#,
);
}
}

View File

@ -5,7 +5,7 @@ use syntax::{
SyntaxKind::WHITESPACE,
T,
ast::{self, AstNode, HasName, make},
ted,
ted::{self, Position},
};
use crate::{
@ -223,6 +223,10 @@ fn impl_def_from_trait(
let first_assoc_item =
add_trait_assoc_items_to_impl(sema, &trait_items, trait_, &impl_def, &target_scope);
if trait_.is_unsafe(sema.db) {
ted::insert(Position::first_child_of(impl_def.syntax()), make::token(T![unsafe]));
}
// Generate a default `impl` function body for the derived trait.
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
let _ = gen_trait_fn_body(func, trait_path, adt, None);

View File

@ -24,7 +24,7 @@ use syntax::{
make,
syntax_factory::SyntaxFactory,
},
ted::{self, Position},
ted,
};
use crate::assist_context::{AssistContext, SourceChangeBuilder};
@ -212,9 +212,6 @@ pub fn add_trait_assoc_items_to_impl(
});
let assoc_item_list = impl_.get_or_create_assoc_item_list();
if trait_.is_unsafe(sema.db) {
ted::insert(Position::first_child_of(impl_.syntax()), make::token(T![unsafe]));
}
let mut first_item = None;
for item in items {