Skip to content

Commit 457eede

Browse files
committed
Add gen default for tuple structs
1 parent 423860c commit 457eede

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn, annotated_name: &ast::Name) ->
279279

280280
/// Generate a `Debug` impl based on the fields and members of the target type.
281281
fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
282-
match adt {
282+
return match adt {
283283
// `Debug` cannot be derived for unions, so no default impl can be provided.
284284
ast::Adt::Union(_) => Some(()),
285285
// Deriving `Debug` for enums is not stable yet.
@@ -289,10 +289,7 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
289289
Some(ast::FieldList::RecordFieldList(field_list)) => {
290290
let mut fields = vec![];
291291
for field in field_list.fields() {
292-
let trait_name = make::ext::ident_path("Default");
293-
let method_name = make::ext::ident_path("default");
294-
let fn_name = make::expr_path(make::path_concat(trait_name, method_name));
295-
let method_call = make::expr_call(fn_name, make::arg_list(None));
292+
let method_call = gen_default_call();
296293
let name_ref = make::name_ref(&field.name()?.to_string());
297294
let field = make::record_expr_field(name_ref, Some(method_call));
298295
fields.push(field);
@@ -302,23 +299,27 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
302299
make::record_expr(struct_name, fields).into()
303300
}
304301
Some(ast::FieldList::TupleFieldList(field_list)) => {
305-
let mut fields = vec![];
306-
for _ in field_list.fields() {
307-
let trait_name = make::ext::ident_path("Default");
308-
let method_name = make::ext::ident_path("default");
309-
let fn_name = make::expr_path(make::path_concat(trait_name, method_name));
310-
let method_call = make::expr_call(fn_name, make::arg_list(None));
311-
fields.push(method_call);
312-
}
313302
let struct_name = make::expr_path(make::ext::ident_path("Self"));
303+
let fields = field_list.fields().map(|_| gen_default_call());
314304
make::expr_call(struct_name, make::arg_list(fields))
315305
}
316-
None => todo!(),
306+
None => {
307+
let struct_name = make::ext::ident_path("Self");
308+
let fields = make::record_expr_field_list(None);
309+
make::record_expr(struct_name, fields).into()
310+
}
317311
};
318312
let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1));
319313
ted::replace(func.body()?.syntax(), body.clone_for_update().syntax());
320-
Some(())
314+
return Some(());
321315
}
316+
};
317+
318+
fn gen_default_call() -> ast::Expr {
319+
let trait_name = make::ext::ident_path("Default");
320+
let method_name = make::ext::ident_path("default");
321+
let fn_name = make::expr_path(make::path_concat(trait_name, method_name));
322+
make::expr_call(fn_name, make::arg_list(None))
322323
}
323324
}
324325
fn update_attribute(
@@ -494,6 +495,26 @@ impl Default for Foo {
494495
Self(Default::default())
495496
}
496497
}
498+
"#,
499+
)
500+
}
501+
#[test]
502+
fn add_custom_impl_default_empty_struct() {
503+
check_assist(
504+
replace_derive_with_manual_impl,
505+
r#"
506+
//- minicore: default
507+
#[derive(Defau$0lt)]
508+
struct Foo;
509+
"#,
510+
r#"
511+
struct Foo;
512+
513+
impl Default for Foo {
514+
$0fn default() -> Self {
515+
Self { }
516+
}
517+
}
497518
"#,
498519
)
499520
}

crates/test_utils/src/minicore.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,6 @@ pub mod fmt {
346346
}
347347
// endregion:fmt
348348

349-
// region:default
350-
pub mod default {
351-
pub trait Default {
352-
fn default() -> Self;
353-
}
354-
}
355-
// endregion:default
356-
357349
// region:slice
358350
pub mod slice {
359351
#[lang = "slice"]

0 commit comments

Comments
 (0)