Observe unsafeness when generating manual impls of former derives

This commit is contained in:
Ali Bektas 2025-03-08 14:13:51 +01:00
parent 0c1b4838ce
commit 477b987179
3 changed files with 35 additions and 2 deletions

View File

@ -2363,4 +2363,33 @@ 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

@ -24,7 +24,7 @@ use syntax::{
make,
syntax_factory::SyntaxFactory,
},
ted,
ted::{self, Position},
};
use crate::assist_context::{AssistContext, SourceChangeBuilder};
@ -212,6 +212,10 @@ 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 {
first_item.get_or_insert_with(|| item.clone());

View File

@ -1276,7 +1276,7 @@ pub mod tokens {
pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| {
SourceFile::parse(
"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: {}",
"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: {}",
Edition::CURRENT,
)
});