|
1 | 1 | use either::Either;
|
2 |
| -use hir::{db::AstDatabase, InFile}; |
3 |
| -use ide_db::{assists::Assist, source_change::SourceChange}; |
| 2 | +use hir::{db::AstDatabase, InFile, Type}; |
| 3 | +use ide_db::{assists::Assist, helpers::FamousDefs, source_change::SourceChange}; |
4 | 4 | use rustc_hash::FxHashMap;
|
5 | 5 | use stdx::format_to;
|
6 | 6 | use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
|
@@ -63,17 +63,30 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
|
63 | 63 | }
|
64 | 64 | });
|
65 | 65 | let missing_fields = ctx.sema.record_literal_missing_fields(&field_list_parent);
|
| 66 | + |
| 67 | + let generate_default_expr = |ty: &Type| { |
| 68 | + let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate(); |
| 69 | + let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default(); |
| 70 | + |
| 71 | + match default_trait { |
| 72 | + Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => { |
| 73 | + Some(make::ext::expr_default()) |
| 74 | + } |
| 75 | + _ => Some(make::ext::expr_todo()), |
| 76 | + } |
| 77 | + }; |
| 78 | + |
66 | 79 | for (f, ty) in missing_fields.iter() {
|
67 | 80 | let field_expr = if let Some(local_candidate) = locals.get(&f.name(ctx.sema.db)) {
|
68 | 81 | cov_mark::hit!(field_shorthand);
|
69 | 82 | let candidate_ty = local_candidate.ty(ctx.sema.db);
|
70 | 83 | if ty.could_unify_with(ctx.sema.db, &candidate_ty) {
|
71 | 84 | None
|
72 | 85 | } else {
|
73 |
| - Some(make::ext::expr_todo()) |
| 86 | + generate_default_expr(ty) |
74 | 87 | }
|
75 | 88 | } else {
|
76 |
| - Some(make::ext::expr_todo()) |
| 89 | + generate_default_expr(ty) |
77 | 90 | };
|
78 | 91 | let field =
|
79 | 92 | make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)
|
|
0 commit comments