Skip to content

Commit 1f709d5

Browse files
committed
Auto merge of rust-lang#12143 - bnjjj:master, r=Veykril
improve the default constructor mode when filling fields Instead of filling a boolean field with `bool::default()` it's not `false` and same for `Option` instead of using `Option::default()` it will be `None`
2 parents 9ed4af8 + 6344eea commit 1f709d5

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,10 @@ impl BuiltinType {
17801780
matches!(self.inner, hir_def::builtin_type::BuiltinType::Char)
17811781
}
17821782

1783+
pub fn is_bool(&self) -> bool {
1784+
matches!(self.inner, hir_def::builtin_type::BuiltinType::Bool)
1785+
}
1786+
17831787
pub fn is_str(&self) -> bool {
17841788
matches!(self.inner, hir_def::builtin_type::BuiltinType::Str)
17851789
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ fn get_default_constructor(
172172
if builtin_ty.is_str() {
173173
return Some(make::ext::empty_str());
174174
}
175+
if builtin_ty.is_bool() {
176+
return Some(make::ext::default_bool());
177+
}
175178
}
176179

177180
let krate = ctx.sema.to_module_def(d.file.original_file(ctx.sema.db))?.krate();
@@ -192,10 +195,13 @@ fn get_default_constructor(
192195
})
193196
.is_some();
194197

198+
let famous_defs = FamousDefs(&ctx.sema, krate);
195199
if has_new_func {
196200
Some(make::ext::expr_ty_new(&make_ty(ty, ctx.sema.db, module)))
201+
} else if ty.as_adt() == famous_defs.core_option_Option()?.ty(ctx.sema.db).as_adt() {
202+
Some(make::ext::option_none())
197203
} else if !ty.is_array()
198-
&& ty.impls_trait(ctx.sema.db, FamousDefs(&ctx.sema, krate).core_default_Default()?, &[])
204+
&& ty.impls_trait(ctx.sema.db, famous_defs.core_default_Default()?, &[])
199205
{
200206
Some(make::ext::expr_ty_default(&make_ty(ty, ctx.sema.db, module)))
201207
} else {
@@ -295,17 +301,18 @@ pub struct Foo { pub a: i32, pub b: i32 }
295301
fn test_fill_struct_fields_empty() {
296302
check_fix(
297303
r#"
298-
struct TestStruct { one: i32, two: i64 }
304+
//- minicore: option
305+
struct TestStruct { one: i32, two: i64, three: Option<i32>, four: bool }
299306
300307
fn test_fn() {
301308
let s = TestStruct {$0};
302309
}
303310
"#,
304311
r#"
305-
struct TestStruct { one: i32, two: i64 }
312+
struct TestStruct { one: i32, two: i64, three: Option<i32>, four: bool }
306313
307314
fn test_fn() {
308-
let s = TestStruct { one: 0, two: 0 };
315+
let s = TestStruct { one: 0, two: 0, three: None, four: false };
309316
}
310317
"#,
311318
);
@@ -415,7 +422,7 @@ fn test_fn() {
415422
fn test_fill_struct_fields_default() {
416423
check_fix(
417424
r#"
418-
//- minicore: default
425+
//- minicore: default, option
419426
struct TestWithDefault(usize);
420427
impl Default for TestWithDefault {
421428
pub fn default() -> Self {

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ pub mod ext {
8181
pub fn default_bool() -> ast::Expr {
8282
expr_from_text("false")
8383
}
84+
pub fn option_none() -> ast::Expr {
85+
expr_from_text("None")
86+
}
8487
pub fn empty_block_expr() -> ast::BlockExpr {
8588
block_expr(None, None)
8689
}

0 commit comments

Comments
 (0)