about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBenjamin Coenen <5719034+bnjjj@users.noreply.github.com>2021-12-30 15:49:31 +0100
committerBenjamin Coenen <5719034+bnjjj@users.noreply.github.com>2021-12-30 15:49:31 +0100
commit0435463439471a81477ccdc0313f312abb14331e (patch)
tree2376b5090d3be60a212cd2a9cb51c24ea74c8da5
parent3d63abf1d868218613f33dc59968d3671f7f14a8 (diff)
downloadrust-0435463439471a81477ccdc0313f312abb14331e.tar.gz
rust-0435463439471a81477ccdc0313f312abb14331e.zip
feat(diagnostics): use Default::default() expression instead of todo! when missing fields
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
-rw-r--r--crates/ide_diagnostics/src/handlers/missing_fields.rs21
-rw-r--r--crates/syntax/src/ast/make.rs3
2 files changed, 20 insertions, 4 deletions
diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs
index 8d17a7e714a..715ac8f9740 100644
--- a/crates/ide_diagnostics/src/handlers/missing_fields.rs
+++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs
@@ -1,6 +1,6 @@
 use either::Either;
-use hir::{db::AstDatabase, InFile};
-use ide_db::{assists::Assist, source_change::SourceChange};
+use hir::{db::AstDatabase, InFile, Type};
+use ide_db::{assists::Assist, helpers::FamousDefs, source_change::SourceChange};
 use rustc_hash::FxHashMap;
 use stdx::format_to;
 use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
@@ -63,6 +63,19 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
         }
     });
     let missing_fields = ctx.sema.record_literal_missing_fields(&field_list_parent);
+
+    let generate_default_expr = |ty: &Type| {
+        let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate();
+        let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
+
+        match default_trait {
+            Some(default_trait) if ty.impls_trait(ctx.sema.db, default_trait, &[]) => {
+                Some(make::ext::expr_default())
+            }
+            _ => Some(make::ext::expr_todo()),
+        }
+    };
+
     for (f, ty) in missing_fields.iter() {
         let field_expr = if let Some(local_candidate) = locals.get(&f.name(ctx.sema.db)) {
             cov_mark::hit!(field_shorthand);
@@ -70,10 +83,10 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
             if ty.could_unify_with(ctx.sema.db, &candidate_ty) {
                 None
             } else {
-                Some(make::ext::expr_todo())
+                generate_default_expr(ty)
             }
         } else {
-            Some(make::ext::expr_todo())
+            generate_default_expr(ty)
         };
         let field =
             make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index e1938307cf3..1a46b84c966 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -59,6 +59,9 @@ pub mod ext {
     pub fn expr_todo() -> ast::Expr {
         expr_from_text("todo!()")
     }
+    pub fn expr_default() -> ast::Expr {
+        expr_from_text("Default::default()")
+    }
     pub fn empty_block_expr() -> ast::BlockExpr {
         block_expr(None, None)
     }