From 03fe6b38b88b3d4f7f79912e7bf1b5fb14d7bbc2 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 13 Dec 2019 17:35:15 -0500 Subject: Remove some unwraps in add_new --- crates/ra_assists/src/assists/add_new.rs | 35 +++++++++++++++----------------- 1 file 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( let struct_ty = { let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() }; - hir::Struct::from_source(db, src).unwrap().ty(db) + hir::Struct::from_source(db, src)?.ty(db) }; - let mut found_new_fn = false; - - let block = module.descendants().filter_map(ast::ImplBlock::cast).find(|impl_blk| { - if found_new_fn { - return false; - } - + let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| { let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() }; - let blk = hir::ImplBlock::from_source(db, src).unwrap(); + let blk = hir::ImplBlock::from_source(db, src)?; let same_ty = blk.target_ty(db) == struct_ty; let not_trait_impl = blk.target_trait(db).is_none(); if !(same_ty && not_trait_impl) { - return false; + None + } else { + Some(impl_blk) } - - found_new_fn = has_new_fn(impl_blk); - true }); - if found_new_fn { - None - } else { - Some(block) + if let Some(ref impl_blk) = block { + if has_new_fn(impl_blk) { + return None; + } } + + Some(block) } fn has_new_fn(imp: &ast::ImplBlock) -> bool { if let Some(il) = imp.item_list() { for item in il.impl_items() { if let ast::ImplItem::FnDef(f) = item { - if f.name().unwrap().text().eq_ignore_ascii_case("new") { - return true; + if let Some(name) = f.name() { + if name.text().eq_ignore_ascii_case("new") { + return true; + } } } } -- cgit v1.2.3