aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorChetan Khilosiya <[email protected]>2021-03-06 19:21:48 +0000
committerChetan Khilosiya <[email protected]>2021-03-06 19:21:48 +0000
commite4b6541c7a86f387c1b60f36df2177047907048e (patch)
treed6e026518695e06b64d9d0603e1bc0f0a27d4fe4 /crates
parent9a84daf47d259380ae0fba21a54853a0fa384cb1 (diff)
7708: handle both FamousDefs fixture and plain code.
Also fix typo in example.
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/generate_default_from_new.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/crates/ide_assists/src/handlers/generate_default_from_new.rs b/crates/ide_assists/src/handlers/generate_default_from_new.rs
index 2eb0d6aad..82f05c65d 100644
--- a/crates/ide_assists/src/handlers/generate_default_from_new.rs
+++ b/crates/ide_assists/src/handlers/generate_default_from_new.rs
@@ -53,8 +53,7 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
53 } 53 }
54 54
55 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?; 55 let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
56 let implements_default = is_default_implemented(ctx, &impl_)?; 56 if is_default_implemented(ctx, &impl_) {
57 if implements_default {
58 return None; 57 return None;
59 } 58 }
60 59
@@ -84,14 +83,25 @@ impl Default for {} {{
84 ) 83 )
85} 84}
86 85
87fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> Option<bool> { 86fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
88 let db = ctx.sema.db; 87 let db = ctx.sema.db;
89 let impl_def = ctx.sema.to_def(impl_)?; 88 let impl_ = ctx.sema.to_def(impl_);
89 let impl_def;
90 match impl_ {
91 Some(value) => impl_def = value,
92 None => return false,
93 }
94
90 let ty = impl_def.target_ty(db); 95 let ty = impl_def.target_ty(db);
91 let krate = impl_def.module(db).krate(); 96 let krate = impl_def.module(db).krate();
92 let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default()?; 97 let default = FamousDefs(&ctx.sema, Some(krate)).core_default_Default();
93 let implements_default = ty.impls_trait(db, default_trait, &[]); 98 let default_trait;
94 Some(implements_default) 99 match default {
100 Some(value) => default_trait = value,
101 None => return false,
102 }
103
104 ty.impls_trait(db, default_trait, &[])
95} 105}
96 106
97#[cfg(test)] 107#[cfg(test)]
@@ -205,7 +215,7 @@ impl Exmaple {
205 r#" 215 r#"
206struct Example { _inner: () } 216struct Example { _inner: () }
207 217
208impl Exmaple { 218impl Example {
209 pub fn n$0ew() -> Self { 219 pub fn n$0ew() -> Self {
210 Self { _inner: () } 220 Self { _inner: () }
211 } 221 }