diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index bc56e03428..f61ab1f410 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,27 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option, rhs: Box) -> Expr { - Expr::Bin { lhs: (), rhs: () } + Expr::Bin { lhs, rhs } } } "#, @@ -324,6 +343,94 @@ fn f() { ); } + #[test] + fn test_fill_struct_fields_shorthand() { + cov_mark::check!(field_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.