From dcd415742095e0d21ce9c4b774ed3433858bf11a Mon Sep 17 00:00:00 2001 From: "nathan.whitaker" Date: Sun, 29 Aug 2021 13:12:59 -0400 Subject: [PATCH 1/4] Use struct init shorthand in fill struct fields assist --- .../src/handlers/missing_fields.rs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index bc56e03428..616f49a3fa 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -1,6 +1,7 @@ use either::Either; use hir::{db::AstDatabase, InFile}; use ide_db::{assists::Assist, source_change::SourceChange}; +use rustc_hash::FxHashMap; use stdx::format_to; use syntax::{algo, ast::make, AstNode, SyntaxNodePtr}; use text_edit::TextEdit; @@ -54,9 +55,26 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option Date: Sun, 29 Aug 2021 13:14:03 -0400 Subject: [PATCH 2/4] Add tests for fill struct fields shorthand --- .../src/handlers/missing_fields.rs | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index 616f49a3fa..d15da23c4a 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -342,6 +342,93 @@ fn f() { ); } + #[test] + fn test_fill_struct_fields_shorthand() { + check_fix( + r#" +struct S { a: &'static str, b: i32 } + +fn f() { + let a = "hello"; + let b = 1i32; + S { + $0 + }; +} +"#, + r#" +struct S { a: &'static str, b: i32 } + +fn f() { + let a = "hello"; + let b = 1i32; + S { + a, + b, + }; +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_shorthand_ty_mismatch() { + check_fix( + r#" +struct S { a: &'static str, b: i32 } + +fn f() { + let a = "hello"; + let b = 1usize; + S { + $0 + }; +} +"#, + r#" +struct S { a: &'static str, b: i32 } + +fn f() { + let a = "hello"; + let b = 1usize; + S { + a, + b: (), + }; +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_shorthand_unifies() { + check_fix( + r#" +struct S { a: &'static str, b: T } + +fn f() { + let a = "hello"; + let b = 1i32; + S { + $0 + }; +} +"#, + r#" +struct S { a: &'static str, b: T } + +fn f() { + let a = "hello"; + let b = 1i32; + S { + a, + b, + }; +} +"#, + ); + } + #[test] fn import_extern_crate_clash_with_inner_item() { // This is more of a resolver test, but doesn't really work with the hir_def testsuite. From e4757f94072e0d5d24107a73f5e5afad15f707cb Mon Sep 17 00:00:00 2001 From: "nathan.whitaker" Date: Sun, 29 Aug 2021 13:14:15 -0400 Subject: [PATCH 3/4] Update existing fill struct fields test --- crates/ide_diagnostics/src/handlers/missing_fields.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index d15da23c4a..cc0b998068 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -242,7 +242,7 @@ enum Expr { impl Expr { fn new_bin(lhs: Box, rhs: Box) -> Expr { - Expr::Bin { lhs: (), rhs: () } + Expr::Bin { lhs, rhs } } } "#, From e1d86a42fee92f261956b12cb2310f1e5904dacf Mon Sep 17 00:00:00 2001 From: "nathan.whitaker" Date: Mon, 30 Aug 2021 14:37:03 -0400 Subject: [PATCH 4/4] Add coverage mark for struct init shorthand test --- crates/ide_diagnostics/src/handlers/missing_fields.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index cc0b998068..f61ab1f410 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -64,6 +64,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option