Skip to content

Commit 3dcfde9

Browse files
Check coercion, not unification, in "Fill struct fields", as the criteria to use an existing local as the field's value
Since struct literals allow coercions.
1 parent 49094db commit 3dcfde9

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
120120
let field_expr = if let Some(local_candidate) = locals.get(&f.name(ctx.sema.db)) {
121121
cov_mark::hit!(field_shorthand);
122122
let candidate_ty = local_candidate.ty(ctx.sema.db);
123-
if ty.could_unify_with(ctx.sema.db, &candidate_ty) {
123+
if candidate_ty.could_coerce_to(ctx.sema.db, ty) {
124124
None
125125
} else {
126126
Some(generate_fill_expr(ty))
@@ -934,4 +934,30 @@ fn main() {
934934
"#,
935935
);
936936
}
937+
938+
#[test]
939+
fn coerce_existing_local() {
940+
check_fix(
941+
r#"
942+
struct A {
943+
v: f64,
944+
}
945+
946+
fn f() -> A {
947+
let v = loop {};
948+
A {$0}
949+
}
950+
"#,
951+
r#"
952+
struct A {
953+
v: f64,
954+
}
955+
956+
fn f() -> A {
957+
let v = loop {};
958+
A { v }
959+
}
960+
"#,
961+
);
962+
}
937963
}

0 commit comments

Comments
 (0)