From de08d30b80f80ee0eaecf4dddb72e6fb829a46b6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 7 Dec 2019 11:52:20 +0100 Subject: [PATCH] Get rid of unwraps in add_new Probably fixes #2464. --- crates/ra_assists/src/assists/add_new.rs | 41 +++++++++++------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs index f977547fb9..d340cac8fd 100644 --- a/crates/ra_assists/src/assists/add_new.rs +++ b/crates/ra_assists/src/assists/add_new.rs @@ -56,42 +56,39 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option { let vis = vis.as_ref().map(String::as_str).unwrap_or(""); write!(&mut buf, " {}fn new(", vis).unwrap(); - join(field_list.fields().map(|f| { - format!( - "{}: {}", - f.name().unwrap().syntax().text(), - f.ascribed_type().unwrap().syntax().text() - ) + join(field_list.fields().filter_map(|f| { + Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text())) })) .separator(", ") .to_buf(&mut buf); buf.push_str(") -> Self { Self {"); - join(field_list.fields().map(|f| f.name().unwrap().syntax().text())) + join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text()))) .separator(", ") .surround_with(" ", " ") .to_buf(&mut buf); buf.push_str("} }"); - let (start_offset, end_offset) = if let Some(impl_block) = impl_block { - buf.push('\n'); - let start = impl_block - .syntax() - .descendants_with_tokens() - .find(|t| t.kind() == T!['{']) - .unwrap() - .text_range() - .end(); + let (start_offset, end_offset) = impl_block + .and_then(|impl_block| { + buf.push('\n'); + let start = impl_block + .syntax() + .descendants_with_tokens() + .find(|t| t.kind() == T!['{'])? + .text_range() + .end(); - (start, TextUnit::from_usize(1)) - } else { - buf = generate_impl_text(&strukt, &buf); - let start = strukt.syntax().text_range().end(); + Some((start, TextUnit::from_usize(1))) + }) + .unwrap_or_else(|| { + buf = generate_impl_text(&strukt, &buf); + let start = strukt.syntax().text_range().end(); - (start, TextUnit::from_usize(3)) - }; + (start, TextUnit::from_usize(3)) + }); edit.set_cursor(start_offset + TextUnit::of_str(&buf) - end_offset); edit.insert(start_offset, buf);