@@ -7,6 +7,7 @@ use syntax::{
77 ast:: { self , Impl , NameOwner } ,
88 AstNode ,
99} ;
10+ use crate :: utils:: generate_trait_impl_text;
1011
1112// Assist: generate_default_from_new
1213//
@@ -59,13 +60,21 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
5960 }
6061
6162 let insert_location = impl_. syntax ( ) . text_range ( ) ;
62-
63+ let code = match ast:: Struct :: cast ( impl_. self_ty ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) {
64+ None => {
65+ default_fn_node_for_new ( impl_)
66+ }
67+ Some ( strukt) => {
68+ generate_trait_impl_text ( & ast:: Adt :: Struct ( strukt) , "core:default:Default" , " fn default() -> Self {{
69+ Self::new()
70+ }}" )
71+ }
72+ } ;
6373 acc. add (
6474 AssistId ( "generate_default_from_new" , crate :: AssistKind :: Generate ) ,
6575 "Generate a Default impl from a new fn" ,
6676 insert_location,
6777 move |builder| {
68- let code = default_fn_node_for_new ( impl_) ;
6978 builder. insert ( insert_location. end ( ) , code) ;
7079 } ,
7180 )
@@ -175,6 +184,40 @@ impl Default for Test {
175184 ) ;
176185 }
177186
187+ #[ test]
188+ fn generate_default3 ( ) {
189+ check_pass (
190+ r#"
191+ pub struct Foo<T> {
192+ _bar: *mut T,
193+ }
194+
195+ impl<T> Foo<T> {
196+ pub fn ne$0w() -> Self {
197+ todo!()
198+ }
199+ }
200+ "# ,
201+ r#"
202+ pub struct Foo<T> {
203+ _bar: *mut T,
204+ }
205+
206+ impl<T> Foo<T> {
207+ pub fn new() -> Self {
208+ todo!()
209+ }
210+ }
211+
212+ impl<T> Default for Foo<T> {
213+ fn default() -> Self {
214+ Self::new()
215+ }
216+ }
217+ "# ,
218+ ) ;
219+ }
220+
178221 #[ test]
179222 fn new_function_with_parameters ( ) {
180223 cov_mark:: check!( new_function_with_parameters) ;
0 commit comments