clean up match in ide_completion::completions::record::complete_record_literal
use original instead of adjusted type in ide_completion::completions::record::complete_record
don't even bother checking if we can complete union literals to Default or to struct update syntax
@@ -15,14 +15,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
) => {
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
- let default_trait = ctx.famous_defs().core_default_Default();
- let impl_default_trait =
- default_trait.zip(ty.as_ref()).map_or(false, |(default_trait, ty)| {
- ty.original.impls_trait(ctx.db, default_trait, &[])
- });
-
- let missing_fields = match ty.and_then(|t| t.adjusted().as_adt()) {
- Some(hir::Adt::Union(un)) => {
+ if let Some(hir::Adt::Union(un)) = ty.as_ref().and_then(|t| t.original.as_adt()) {
// ctx.sema.record_literal_missing_fields will always return
// an empty Vec on a union literal. This is normally
// reasonable, but here we'd like to present the full list
@@ -36,9 +29,15 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
false => un.fields(ctx.db).into_iter().map(|f| (f, f.ty(ctx.db))).collect(),
true => vec![],
}
- }
- _ => ctx.sema.record_literal_missing_fields(record_expr),
- };
+ } else {
+ let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
+
+ let default_trait = ctx.famous_defs().core_default_Default();
+ let impl_default_trait =
+ default_trait.zip(ty.as_ref()).map_or(false, |(default_trait, ty)| {
+ ty.original.impls_trait(ctx.db, default_trait, &[])
+ });
+
if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
let completion_text = "..Default::default()";
let mut item =
@@ -60,6 +59,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
}
missing_fields
}
+ }
Some(ImmediateLocation::RecordPat(record_pat)) => {
ctx.sema.record_pattern_missing_fields(record_pat)
}
@@ -82,23 +82,18 @@ pub(crate) fn complete_record_literal(
}
match ctx.expected_type.as_ref()?.as_adt()? {
- hir::Adt::Struct(strukt) => {
- if ctx.path_qual().is_none() {
- let module =
- if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
+ hir::Adt::Struct(strukt) if ctx.path_qual().is_none() => {
+ let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
acc.add_struct_literal(ctx, strukt, path, None);
}
- }
- hir::Adt::Union(un) => {
- if ctx.path_qual().is_none() {
+ hir::Adt::Union(un) if ctx.path_qual().is_none() => {
let module = if let Some(module) = ctx.module { module } else { un.module(ctx.db) };
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(un));
acc.add_union_literal(ctx, un, path, None);
}
- }
_ => {}
};
