11145: feat: add config to use reasonable default expression instead of todo! when filling missing fields r=Veykril a=bnjjj

Use `Default::default()` in struct fields when we ask to fill it instead of putting `todo!()` for every fields

before:

```rust
pub enum Other {
    One,
    Two,
}

pub struct Test {
    text: String,
    num: usize,
    other: Other,
}

fn t_test() {
    let test = Test {<|>};
}
``` 

after: 

```rust
pub enum Other {
    One,
    Two,
}

pub struct Test {
    text: String,
    num: usize,
    other: Other,
}

fn t_test() {
    let test = Test {
        text: String::new(),
        num: 0,
        other: todo!(),
    };
}
``` 



Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
Co-authored-by: Coenen Benjamin <benjamin.coenen@hotmail.com>
This commit is contained in:
bors[bot] 2022-01-07 14:10:11 +00:00 committed by GitHub
commit 40009e07d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 271 additions and 25 deletions

View file

@ -1689,6 +1689,26 @@ impl BuiltinType {
pub fn name(self) -> Name {
self.inner.as_name()
}
pub fn is_int(&self) -> bool {
matches!(self.inner, hir_def::builtin_type::BuiltinType::Int(_))
}
pub fn is_uint(&self) -> bool {
matches!(self.inner, hir_def::builtin_type::BuiltinType::Uint(_))
}
pub fn is_float(&self) -> bool {
matches!(self.inner, hir_def::builtin_type::BuiltinType::Float(_))
}
pub fn is_char(&self) -> bool {
matches!(self.inner, hir_def::builtin_type::BuiltinType::Char)
}
pub fn is_str(&self) -> bool {
matches!(self.inner, hir_def::builtin_type::BuiltinType::Str)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -2615,6 +2635,10 @@ impl Type {
matches!(&self.ty.kind(Interner), TyKind::FnDef(..) | TyKind::Function { .. })
}
pub fn is_array(&self) -> bool {
matches!(&self.ty.kind(Interner), TyKind::Array(..))
}
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
let adt_id = match *self.ty.kind(Interner) {
TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
@ -2713,7 +2737,7 @@ impl Type {
// This would be nicer if it just returned an iterator, but that runs into
// lifetime problems, because we need to borrow temp `CrateImplDefs`.
pub fn iterate_assoc_items<T>(
self,
&self,
db: &dyn HirDatabase,
krate: Crate,
mut callback: impl FnMut(AssocItem) -> Option<T>,
@ -2727,7 +2751,7 @@ impl Type {
}
fn iterate_assoc_items_dyn(
self,
&self,
db: &dyn HirDatabase,
krate: Crate,
callback: &mut dyn FnMut(AssocItemId) -> bool,
@ -2769,6 +2793,7 @@ impl Type {
) -> Option<T> {
let _p = profile::span("iterate_method_candidates");
let mut slot = None;
self.iterate_method_candidates_dyn(
db,
krate,