aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorkjeremy <[email protected]>2019-12-13 22:35:15 +0000
committerkjeremy <[email protected]>2019-12-13 23:28:01 +0000
commit03fe6b38b88b3d4f7f79912e7bf1b5fb14d7bbc2 (patch)
tree1a107cea2e63e97836d42e2c3b5ac6c48f90694b /crates/ra_assists
parent169fe4932f84f396965a4814c44e31061673937c (diff)
Remove some unwraps in add_new
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assists/add_new.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs
index d340cac8f..b2f946fac 100644
--- a/crates/ra_assists/src/assists/add_new.rs
+++ b/crates/ra_assists/src/assists/add_new.rs
@@ -139,43 +139,40 @@ fn find_struct_impl(
139 139
140 let struct_ty = { 140 let struct_ty = {
141 let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() }; 141 let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() };
142 hir::Struct::from_source(db, src).unwrap().ty(db) 142 hir::Struct::from_source(db, src)?.ty(db)
143 }; 143 };
144 144
145 let mut found_new_fn = false; 145 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| {
146
147 let block = module.descendants().filter_map(ast::ImplBlock::cast).find(|impl_blk| {
148 if found_new_fn {
149 return false;
150 }
151
152 let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() }; 146 let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() };
153 let blk = hir::ImplBlock::from_source(db, src).unwrap(); 147 let blk = hir::ImplBlock::from_source(db, src)?;
154 148
155 let same_ty = blk.target_ty(db) == struct_ty; 149 let same_ty = blk.target_ty(db) == struct_ty;
156 let not_trait_impl = blk.target_trait(db).is_none(); 150 let not_trait_impl = blk.target_trait(db).is_none();
157 151
158 if !(same_ty && not_trait_impl) { 152 if !(same_ty && not_trait_impl) {
159 return false; 153 None
154 } else {
155 Some(impl_blk)
160 } 156 }
161
162 found_new_fn = has_new_fn(impl_blk);
163 true
164 }); 157 });
165 158
166 if found_new_fn { 159 if let Some(ref impl_blk) = block {
167 None 160 if has_new_fn(impl_blk) {
168 } else { 161 return None;
169 Some(block) 162 }
170 } 163 }
164
165 Some(block)
171} 166}
172 167
173fn has_new_fn(imp: &ast::ImplBlock) -> bool { 168fn has_new_fn(imp: &ast::ImplBlock) -> bool {
174 if let Some(il) = imp.item_list() { 169 if let Some(il) = imp.item_list() {
175 for item in il.impl_items() { 170 for item in il.impl_items() {
176 if let ast::ImplItem::FnDef(f) = item { 171 if let ast::ImplItem::FnDef(f) = item {
177 if f.name().unwrap().text().eq_ignore_ascii_case("new") { 172 if let Some(name) = f.name() {
178 return true; 173 if name.text().eq_ignore_ascii_case("new") {
174 return true;
175 }
179 } 176 }
180 } 177 }
181 } 178 }