3700: fill match arms with empty block rather than unit tuple r=matklad a=JoshMcguigan

As requested by @Veetaha in #3689 and #3687, this modifies the fill match arms assist to create match arms as an empty block `{}` rather than a unit tuple `()`.

In one test I left one of the pre-existing match arms as a unit tuple, and added a body to another match arm, to demonstrate that the contents of existing match arms persist. 

Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
bors[bot] 2020-03-24 11:48:32 +00:00 committed by GitHub
commit 6ef64622af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 49 deletions

View file

@ -275,8 +275,8 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) { fn handle(action: Action) {
match action { match action {
Action::Move { distance } => (), Action::Move { distance } => {}
Action::Stop => (), Action::Stop => {}
} }
} }
"#####, "#####,

View file

@ -30,8 +30,8 @@ use ast::{MatchArm, Pat};
// //
// fn handle(action: Action) { // fn handle(action: Action) {
// match action { // match action {
// Action::Move { distance } => (), // Action::Move { distance } => {}
// Action::Stop => (), // Action::Stop => {}
// } // }
// } // }
// ``` // ```
@ -57,7 +57,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
.into_iter() .into_iter()
.filter_map(|variant| build_pat(ctx.db, module, variant)) .filter_map(|variant| build_pat(ctx.db, module, variant))
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat)) .filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
.map(|pat| make::match_arm(iter::once(pat), make::expr_unit())) .map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
.collect() .collect()
} else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) { } else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) {
// Partial fill not currently supported for tuple of enums. // Partial fill not currently supported for tuple of enums.
@ -86,7 +86,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
ast::Pat::from(make::tuple_pat(patterns)) ast::Pat::from(make::tuple_pat(patterns))
}) })
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat)) .filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
.map(|pat| make::match_arm(iter::once(pat), make::expr_unit())) .map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
.collect() .collect()
} else { } else {
return None; return None;
@ -192,8 +192,8 @@ mod tests {
fn main() { fn main() {
match A::As<|> { match A::As<|> {
A::As, A::As,
A::Bs{x,y:Some(_)} => (), A::Bs{x,y:Some(_)} => {}
A::Cs(_, Some(_)) => (), A::Cs(_, Some(_)) => {}
} }
} }
"#, "#,
@ -227,8 +227,8 @@ mod tests {
} }
fn main() { fn main() {
match A::As<|> { match A::As<|> {
A::Bs{x,y:Some(_)} => (), A::Bs{x,y:Some(_)} => {}
A::Cs(_, Some(_)) => (), A::Cs(_, Some(_)) => {}
} }
} }
"#, "#,
@ -240,9 +240,9 @@ mod tests {
} }
fn main() { fn main() {
match <|>A::As { match <|>A::As {
A::Bs{x,y:Some(_)} => (), A::Bs{x,y:Some(_)} => {}
A::Cs(_, Some(_)) => (), A::Cs(_, Some(_)) => {}
A::As => (), A::As => {}
} }
} }
"#, "#,
@ -261,7 +261,7 @@ mod tests {
} }
fn main() { fn main() {
match A::As<|> { match A::As<|> {
A::Cs(_) | A::Bs => (), A::Cs(_) | A::Bs => {}
} }
} }
"#, "#,
@ -273,8 +273,8 @@ mod tests {
} }
fn main() { fn main() {
match <|>A::As { match <|>A::As {
A::Cs(_) | A::Bs => (), A::Cs(_) | A::Bs => {}
A::As => (), A::As => {}
} }
} }
"#, "#,
@ -299,8 +299,8 @@ mod tests {
} }
fn main() { fn main() {
match A::As<|> { match A::As<|> {
A::Bs if 0 < 1 => (), A::Bs if 0 < 1 => {}
A::Ds(_value) => (), A::Ds(_value) => { let x = 1; }
A::Es(B::Xs) => (), A::Es(B::Xs) => (),
} }
} }
@ -319,11 +319,11 @@ mod tests {
} }
fn main() { fn main() {
match <|>A::As { match <|>A::As {
A::Bs if 0 < 1 => (), A::Bs if 0 < 1 => {}
A::Ds(_value) => (), A::Ds(_value) => { let x = 1; }
A::Es(B::Xs) => (), A::Es(B::Xs) => (),
A::As => (), A::As => {}
A::Cs => (), A::Cs => {}
} }
} }
"#, "#,
@ -360,11 +360,11 @@ mod tests {
fn main() { fn main() {
let a = A::As; let a = A::As;
match <|>a { match <|>a {
A::As => (), A::As => {}
A::Bs => (), A::Bs => {}
A::Cs(_) => (), A::Cs(_) => {}
A::Ds(_, _) => (), A::Ds(_, _) => {}
A::Es { x, y } => (), A::Es { x, y } => {}
} }
} }
"#, "#,
@ -405,10 +405,10 @@ mod tests {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match <|>(a, b) { match <|>(a, b) {
(A::One, B::One) => (), (A::One, B::One) => {}
(A::One, B::Two) => (), (A::One, B::Two) => {}
(A::Two, B::One) => (), (A::Two, B::One) => {}
(A::Two, B::Two) => (), (A::Two, B::Two) => {}
} }
} }
"#, "#,
@ -449,10 +449,10 @@ mod tests {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match <|>(&a, &b) { match <|>(&a, &b) {
(A::One, B::One) => (), (A::One, B::One) => {}
(A::One, B::Two) => (), (A::One, B::Two) => {}
(A::Two, B::One) => (), (A::Two, B::One) => {}
(A::Two, B::Two) => (), (A::Two, B::Two) => {}
} }
} }
"#, "#,
@ -477,7 +477,7 @@ mod tests {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match (a<|>, b) { match (a<|>, b) {
(A::Two, B::One) => (), (A::Two, B::One) => {}
} }
} }
"#, "#,
@ -502,10 +502,10 @@ mod tests {
let a = A::One; let a = A::One;
let b = B::One; let b = B::One;
match (a<|>, b) { match (a<|>, b) {
(A::Two, B::One) => (), (A::Two, B::One) => {}
(A::One, B::One) => (), (A::One, B::One) => {}
(A::One, B::Two) => (), (A::One, B::Two) => {}
(A::Two, B::Two) => (), (A::Two, B::Two) => {}
} }
} }
"#, "#,
@ -555,7 +555,7 @@ mod tests {
fn foo(a: &A) { fn foo(a: &A) {
match <|>a { match <|>a {
A::As => (), A::As => {}
} }
} }
"#, "#,
@ -580,7 +580,7 @@ mod tests {
fn foo(a: &mut A) { fn foo(a: &mut A) {
match <|>a { match <|>a {
A::Es { x, y } => (), A::Es { x, y } => {}
} }
} }
"#, "#,
@ -611,7 +611,7 @@ mod tests {
fn main() { fn main() {
match E::X { match E::X {
<|>_ => {}, <|>_ => {}
} }
} }
"#, "#,
@ -620,8 +620,8 @@ mod tests {
fn main() { fn main() {
match <|>E::X { match <|>E::X {
E::X => (), E::X => {}
E::Y => (), E::Y => {}
} }
} }
"#, "#,
@ -648,8 +648,8 @@ mod tests {
fn main() { fn main() {
match <|>X { match <|>X {
X => (), X => {}
foo::E::Y => (), foo::E::Y => {}
} }
} }
"#, "#,

View file

@ -87,6 +87,9 @@ pub fn block_from_expr(e: ast::Expr) -> ast::Block {
pub fn expr_unit() -> ast::Expr { pub fn expr_unit() -> ast::Expr {
expr_from_text("()") expr_from_text("()")
} }
pub fn expr_empty_block() -> ast::Expr {
expr_from_text("{}")
}
pub fn expr_unimplemented() -> ast::Expr { pub fn expr_unimplemented() -> ast::Expr {
expr_from_text("unimplemented!()") expr_from_text("unimplemented!()")
} }

View file

@ -267,8 +267,8 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) { fn handle(action: Action) {
match action { match action {
Action::Move { distance } => (), Action::Move { distance } => {}
Action::Stop => (), Action::Stop => {}
} }
} }
``` ```