Skip to content

Commit 59bcbaf

Browse files
committed
Auto merge of rust-lang#15594 - alibektas:deunwrap/add_missing_match_arms, r=Veykril
Deunwrap add_missing_match_arms Last subtask of rust-lang#15398
2 parents 4a8622c + 622e1a8 commit 59bcbaf

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

crates/ide-assists/src/handlers/add_missing_match_arms.rs

+42-8
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
273273
syntax::SyntaxElement::Token(it) => {
274274
// Don't have a way to make tokens mut, so instead make the parent mut
275275
// and find the token again
276-
let parent = edit.make_syntax_mut(it.parent().unwrap());
276+
let parent =
277+
edit.make_syntax_mut(it.parent().expect("Token must have a parent."));
277278
let mut_token =
278-
parent.covering_element(it.text_range()).into_token().unwrap();
279+
parent.covering_element(it.text_range()).into_token().expect("Covering element cannot be found. Range may be beyond the current node's range");
279280

280281
syntax::SyntaxElement::from(mut_token)
281282
}
@@ -446,21 +447,23 @@ fn build_pat(
446447
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_no_std)?);
447448

448449
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
449-
let pat: ast::Pat = match var.source(db)?.value.kind() {
450+
Some(match var.source(db)?.value.kind() {
450451
ast::StructKind::Tuple(field_list) => {
451452
let pats =
452453
iter::repeat(make::wildcard_pat().into()).take(field_list.fields().count());
453454
make::tuple_struct_pat(path, pats).into()
454455
}
455456
ast::StructKind::Record(field_list) => {
456-
let pats = field_list
457-
.fields()
458-
.map(|f| make::ext::simple_ident_pat(f.name().unwrap()).into());
457+
let pats = field_list.fields().map(|f| {
458+
make::ext::simple_ident_pat(
459+
f.name().expect("Record field must have a name"),
460+
)
461+
.into()
462+
});
459463
make::record_pat(path, pats).into()
460464
}
461465
ast::StructKind::Unit => make::path_pat(path),
462-
};
463-
Some(pat)
466+
})
464467
}
465468
ExtendedVariant::True => Some(ast::Pat::from(make::literal_pat("true"))),
466469
ExtendedVariant::False => Some(ast::Pat::from(make::literal_pat("false"))),
@@ -1941,4 +1944,35 @@ fn main() {
19411944
"#,
19421945
);
19431946
}
1947+
1948+
/// See [`discussion`](https://github.com/rust-lang/rust-analyzer/pull/15594#discussion_r1322960614)
1949+
#[test]
1950+
fn missing_field_name() {
1951+
check_assist(
1952+
add_missing_match_arms,
1953+
r#"
1954+
enum A {
1955+
A,
1956+
Missing { a: u32, : u32, c: u32 }
1957+
}
1958+
1959+
fn a() {
1960+
let b = A::A;
1961+
match b$0 {}
1962+
}"#,
1963+
r#"
1964+
enum A {
1965+
A,
1966+
Missing { a: u32, : u32, c: u32 }
1967+
}
1968+
1969+
fn a() {
1970+
let b = A::A;
1971+
match b {
1972+
$0A::A => todo!(),
1973+
A::Missing { a, u32, c } => todo!(),
1974+
}
1975+
}"#,
1976+
)
1977+
}
19441978
}

0 commit comments

Comments
 (0)