diff options
| -rw-r--r-- | crates/hir/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_fields.rs | 17 | ||||
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 3 |
3 files changed, 19 insertions, 5 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 2d731687277..ac81b8140ba 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1780,6 +1780,10 @@ impl BuiltinType { matches!(self.inner, hir_def::builtin_type::BuiltinType::Char) } + pub fn is_bool(&self) -> bool { + matches!(self.inner, hir_def::builtin_type::BuiltinType::Bool) + } + pub fn is_str(&self) -> bool { matches!(self.inner, hir_def::builtin_type::BuiltinType::Str) } diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs index 3160004380f..891547aaef5 100644 --- a/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -172,6 +172,9 @@ fn get_default_constructor( if builtin_ty.is_str() { return Some(make::ext::empty_str()); } + if builtin_ty.is_bool() { + return Some(make::ext::default_bool()); + } } let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate(); @@ -192,10 +195,13 @@ fn get_default_constructor( }) .is_some(); + let famous_defs = FamousDefs(&ctx.sema, krate); if has_new_func { Some(make::ext::expr_ty_new(&make_ty(ty, ctx.sema.db, module))) + } else if ty.as_adt() == famous_defs.core_option_Option()?.ty(ctx.sema.db).as_adt() { + Some(make::ext::option_none()) } else if !ty.is_array() - && ty.impls_trait(ctx.sema.db, FamousDefs(&ctx.sema, krate).core_default_Default()?, &[]) + && ty.impls_trait(ctx.sema.db, famous_defs.core_default_Default()?, &[]) { Some(make::ext::expr_ty_default(&make_ty(ty, ctx.sema.db, module))) } else { @@ -295,17 +301,18 @@ pub struct Foo { pub a: i32, pub b: i32 } fn test_fill_struct_fields_empty() { check_fix( r#" -struct TestStruct { one: i32, two: i64 } +//- minicore: option +struct TestStruct { one: i32, two: i64, three: Option<i32>, four: bool } fn test_fn() { let s = TestStruct {$0}; } "#, r#" -struct TestStruct { one: i32, two: i64 } +struct TestStruct { one: i32, two: i64, three: Option<i32>, four: bool } fn test_fn() { - let s = TestStruct { one: 0, two: 0 }; + let s = TestStruct { one: 0, two: 0, three: None, four: false }; } "#, ); @@ -415,7 +422,7 @@ fn test_fn() { fn test_fill_struct_fields_default() { check_fix( r#" -//- minicore: default +//- minicore: default, option struct TestWithDefault(usize); impl Default for TestWithDefault { pub fn default() -> Self { diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index d7a7d2d32a5..5908dda8e63 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -81,6 +81,9 @@ pub mod ext { pub fn default_bool() -> ast::Expr { expr_from_text("false") } + pub fn option_none() -> ast::Expr { + expr_from_text("None") + } pub fn empty_block_expr() -> ast::BlockExpr { block_expr(None, None) } |
