Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21971 from ChayimFriedman2/fill-fields-coerce
fix: Check coercion, not unification, in "Fill struct fields", as the criteria to use an existing local as the field's value
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_fields.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs index 050d5477f6..efbd266714 100644 --- a/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -120,7 +120,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass let field_expr = if let Some(local_candidate) = locals.get(&f.name(ctx.sema.db)) { cov_mark::hit!(field_shorthand); let candidate_ty = local_candidate.ty(ctx.sema.db); - if ty.could_unify_with(ctx.sema.db, &candidate_ty) { + if candidate_ty.could_coerce_to(ctx.sema.db, ty) { None } else { Some(generate_fill_expr(ty)) @@ -934,4 +934,30 @@ fn main() { "#, ); } + + #[test] + fn coerce_existing_local() { + check_fix( + r#" +struct A { + v: f64, +} + +fn f() -> A { + let v = loop {}; + A {$0} +} + "#, + r#" +struct A { + v: f64, +} + +fn f() -> A { + let v = loop {}; + A { v } +} + "#, + ); + } } |