Auto merge of #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`
This commit is contained in:
bors 2022-05-04 09:32:17 +00:00
commit 1f709d5446
3 changed files with 19 additions and 5 deletions

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)
}