Skip to content

Commit 96d6940

Browse files
committed
Remove unwraps in "Convert to named struct" assist
1 parent e0a60e7 commit 96d6940

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{Adt, ModuleDef};
1+
use hir::{Adt, ModuleDef, Struct};
22
use ide_db::defs::{Definition, NameRefClass};
33
use syntax::{
44
ast::{self, AstNode, GenericParamsOwner, VisibilityOwner},
@@ -55,6 +55,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct(
5555
ast::FieldList::TupleFieldList(it) => it,
5656
ast::FieldList::RecordFieldList(_) => return None,
5757
};
58+
let strukt_def = ctx.sema.to_def(&strukt)?;
5859

5960
let target = strukt.syntax().text_range();
6061
acc.add(
@@ -64,7 +65,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct(
6465
|edit| {
6566
let names = generate_names(tuple_fields.fields());
6667
edit_field_references(ctx, edit, tuple_fields.fields(), &names);
67-
edit_struct_references(ctx, edit, &strukt, &names);
68+
edit_struct_references(ctx, edit, strukt_def, &names);
6869
edit_struct_def(ctx, edit, &strukt, tuple_fields, names);
6970
},
7071
)
@@ -80,7 +81,7 @@ fn edit_struct_def(
8081
let record_fields = tuple_fields
8182
.fields()
8283
.zip(names)
83-
.map(|(f, name)| ast::make::record_field(f.visibility(), name, f.ty().unwrap()));
84+
.filter_map(|(f, name)| Some(ast::make::record_field(f.visibility(), name, f.ty()?)));
8485
let record_fields = ast::make::record_field_list(record_fields);
8586
let tuple_fields_text_range = tuple_fields.syntax().text_range();
8687

@@ -103,10 +104,9 @@ fn edit_struct_def(
103104
fn edit_struct_references(
104105
ctx: &AssistContext,
105106
edit: &mut AssistBuilder,
106-
strukt: &ast::Struct,
107+
strukt: Struct,
107108
names: &[ast::Name],
108109
) {
109-
let strukt = ctx.sema.to_def(strukt).unwrap();
110110
let strukt_def = Definition::ModuleDef(ModuleDef::Adt(Adt::Struct(strukt)));
111111
let usages = strukt_def.usages(&ctx.sema).include_self_kw_refs(true).all();
112112

@@ -117,10 +117,15 @@ fn edit_struct_references(
117117
match_ast! {
118118
match node {
119119
ast::TupleStructPat(tuple_struct_pat) => {
120+
let path = match tuple_struct_pat.path() {
121+
Some(it) => it,
122+
None => continue,
123+
};
124+
120125
edit.replace(
121126
tuple_struct_pat.syntax().text_range(),
122127
ast::make::record_pat_with_fields(
123-
tuple_struct_pat.path().unwrap(),
128+
path,
124129
ast::make::record_pat_field_list(tuple_struct_pat.fields().zip(names).map(
125130
|(pat, name)| {
126131
ast::make::record_pat_field(
@@ -135,7 +140,10 @@ fn edit_struct_references(
135140
},
136141
// for tuple struct creations like Foo(42)
137142
ast::CallExpr(call_expr) => {
138-
let path = call_expr.syntax().descendants().find_map(ast::PathExpr::cast).unwrap().path().unwrap();
143+
let path = match call_expr.syntax().descendants().find_map(ast::PathExpr::cast).map(|expr| expr.path()) {
144+
Some(Some(it)) => it,
145+
_ => continue,
146+
};
139147

140148
// this also includes method calls like Foo::new(42), we should skip them
141149
if let Some(Some(name_ref)) = path.segment().map(|s| s.name_ref()) {
@@ -146,8 +154,10 @@ fn edit_struct_references(
146154
};
147155
}
148156

149-
let arg_list =
150-
call_expr.syntax().descendants().find_map(ast::ArgList::cast).unwrap();
157+
let arg_list = match call_expr.syntax().descendants().find_map(ast::ArgList::cast) {
158+
Some(it) => it,
159+
None => continue,
160+
};
151161

152162
edit.replace(
153163
call_expr.syntax().text_range(),

0 commit comments

Comments
 (0)