diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-07 10:56:50 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-07 10:56:50 +0000 |
commit | 35fc983dd9e904ad4961b9c10be3397bad33da0c (patch) | |
tree | f46baf6b44f24cb80eac113ca2f1e1737cd5e085 /crates | |
parent | 431836f4a01dda39d10f6275915f9c8e99a28028 (diff) | |
parent | de08d30b80f80ee0eaecf4dddb72e6fb829a46b6 (diff) |
Merge #2490
2490: Get rid of unwraps in add_new r=matklad a=flodiebold
Probably fixes #2464.
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/assists/add_new.rs | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs index f977547fb..d340cac8f 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<impl HirDatabase>) -> Option<Assist> { | |||
56 | let vis = vis.as_ref().map(String::as_str).unwrap_or(""); | 56 | let vis = vis.as_ref().map(String::as_str).unwrap_or(""); |
57 | write!(&mut buf, " {}fn new(", vis).unwrap(); | 57 | write!(&mut buf, " {}fn new(", vis).unwrap(); |
58 | 58 | ||
59 | join(field_list.fields().map(|f| { | 59 | join(field_list.fields().filter_map(|f| { |
60 | format!( | 60 | Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text())) |
61 | "{}: {}", | ||
62 | f.name().unwrap().syntax().text(), | ||
63 | f.ascribed_type().unwrap().syntax().text() | ||
64 | ) | ||
65 | })) | 61 | })) |
66 | .separator(", ") | 62 | .separator(", ") |
67 | .to_buf(&mut buf); | 63 | .to_buf(&mut buf); |
68 | 64 | ||
69 | buf.push_str(") -> Self { Self {"); | 65 | buf.push_str(") -> Self { Self {"); |
70 | 66 | ||
71 | join(field_list.fields().map(|f| f.name().unwrap().syntax().text())) | 67 | join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text()))) |
72 | .separator(", ") | 68 | .separator(", ") |
73 | .surround_with(" ", " ") | 69 | .surround_with(" ", " ") |
74 | .to_buf(&mut buf); | 70 | .to_buf(&mut buf); |
75 | 71 | ||
76 | buf.push_str("} }"); | 72 | buf.push_str("} }"); |
77 | 73 | ||
78 | let (start_offset, end_offset) = if let Some(impl_block) = impl_block { | 74 | let (start_offset, end_offset) = impl_block |
79 | buf.push('\n'); | 75 | .and_then(|impl_block| { |
80 | let start = impl_block | 76 | buf.push('\n'); |
81 | .syntax() | 77 | let start = impl_block |
82 | .descendants_with_tokens() | 78 | .syntax() |
83 | .find(|t| t.kind() == T!['{']) | 79 | .descendants_with_tokens() |
84 | .unwrap() | 80 | .find(|t| t.kind() == T!['{'])? |
85 | .text_range() | 81 | .text_range() |
86 | .end(); | 82 | .end(); |
87 | 83 | ||
88 | (start, TextUnit::from_usize(1)) | 84 | Some((start, TextUnit::from_usize(1))) |
89 | } else { | 85 | }) |
90 | buf = generate_impl_text(&strukt, &buf); | 86 | .unwrap_or_else(|| { |
91 | let start = strukt.syntax().text_range().end(); | 87 | buf = generate_impl_text(&strukt, &buf); |
92 | 88 | let start = strukt.syntax().text_range().end(); | |
93 | (start, TextUnit::from_usize(3)) | 89 | |
94 | }; | 90 | (start, TextUnit::from_usize(3)) |
91 | }); | ||
95 | 92 | ||
96 | edit.set_cursor(start_offset + TextUnit::of_str(&buf) - end_offset); | 93 | edit.set_cursor(start_offset + TextUnit::of_str(&buf) - end_offset); |
97 | edit.insert(start_offset, buf); | 94 | edit.insert(start_offset, buf); |