Skip to content

Commit 6ef6462

Browse files
Merge #3700
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>
2 parents 8617fe6 + ff62ef1 commit 6ef6462

File tree

4 files changed

+52
-49
lines changed

4 files changed

+52
-49
lines changed

crates/ra_assists/src/doc_tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ enum Action { Move { distance: u32 }, Stop }
275275
276276
fn handle(action: Action) {
277277
match action {
278-
Action::Move { distance } => (),
279-
Action::Stop => (),
278+
Action::Move { distance } => {}
279+
Action::Stop => {}
280280
}
281281
}
282282
"#####,

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use ast::{MatchArm, Pat};
3030
//
3131
// fn handle(action: Action) {
3232
// match action {
33-
// Action::Move { distance } => (),
34-
// Action::Stop => (),
33+
// Action::Move { distance } => {}
34+
// Action::Stop => {}
3535
// }
3636
// }
3737
// ```
@@ -57,7 +57,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
5757
.into_iter()
5858
.filter_map(|variant| build_pat(ctx.db, module, variant))
5959
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
60-
.map(|pat| make::match_arm(iter::once(pat), make::expr_unit()))
60+
.map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
6161
.collect()
6262
} else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) {
6363
// Partial fill not currently supported for tuple of enums.
@@ -86,7 +86,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
8686
ast::Pat::from(make::tuple_pat(patterns))
8787
})
8888
.filter(|variant_pat| is_variant_missing(&mut arms, variant_pat))
89-
.map(|pat| make::match_arm(iter::once(pat), make::expr_unit()))
89+
.map(|pat| make::match_arm(iter::once(pat), make::expr_empty_block()))
9090
.collect()
9191
} else {
9292
return None;
@@ -192,8 +192,8 @@ mod tests {
192192
fn main() {
193193
match A::As<|> {
194194
A::As,
195-
A::Bs{x,y:Some(_)} => (),
196-
A::Cs(_, Some(_)) => (),
195+
A::Bs{x,y:Some(_)} => {}
196+
A::Cs(_, Some(_)) => {}
197197
}
198198
}
199199
"#,
@@ -227,8 +227,8 @@ mod tests {
227227
}
228228
fn main() {
229229
match A::As<|> {
230-
A::Bs{x,y:Some(_)} => (),
231-
A::Cs(_, Some(_)) => (),
230+
A::Bs{x,y:Some(_)} => {}
231+
A::Cs(_, Some(_)) => {}
232232
}
233233
}
234234
"#,
@@ -240,9 +240,9 @@ mod tests {
240240
}
241241
fn main() {
242242
match <|>A::As {
243-
A::Bs{x,y:Some(_)} => (),
244-
A::Cs(_, Some(_)) => (),
245-
A::As => (),
243+
A::Bs{x,y:Some(_)} => {}
244+
A::Cs(_, Some(_)) => {}
245+
A::As => {}
246246
}
247247
}
248248
"#,
@@ -261,7 +261,7 @@ mod tests {
261261
}
262262
fn main() {
263263
match A::As<|> {
264-
A::Cs(_) | A::Bs => (),
264+
A::Cs(_) | A::Bs => {}
265265
}
266266
}
267267
"#,
@@ -273,8 +273,8 @@ mod tests {
273273
}
274274
fn main() {
275275
match <|>A::As {
276-
A::Cs(_) | A::Bs => (),
277-
A::As => (),
276+
A::Cs(_) | A::Bs => {}
277+
A::As => {}
278278
}
279279
}
280280
"#,
@@ -299,8 +299,8 @@ mod tests {
299299
}
300300
fn main() {
301301
match A::As<|> {
302-
A::Bs if 0 < 1 => (),
303-
A::Ds(_value) => (),
302+
A::Bs if 0 < 1 => {}
303+
A::Ds(_value) => { let x = 1; }
304304
A::Es(B::Xs) => (),
305305
}
306306
}
@@ -319,11 +319,11 @@ mod tests {
319319
}
320320
fn main() {
321321
match <|>A::As {
322-
A::Bs if 0 < 1 => (),
323-
A::Ds(_value) => (),
322+
A::Bs if 0 < 1 => {}
323+
A::Ds(_value) => { let x = 1; }
324324
A::Es(B::Xs) => (),
325-
A::As => (),
326-
A::Cs => (),
325+
A::As => {}
326+
A::Cs => {}
327327
}
328328
}
329329
"#,
@@ -360,11 +360,11 @@ mod tests {
360360
fn main() {
361361
let a = A::As;
362362
match <|>a {
363-
A::As => (),
364-
A::Bs => (),
365-
A::Cs(_) => (),
366-
A::Ds(_, _) => (),
367-
A::Es { x, y } => (),
363+
A::As => {}
364+
A::Bs => {}
365+
A::Cs(_) => {}
366+
A::Ds(_, _) => {}
367+
A::Es { x, y } => {}
368368
}
369369
}
370370
"#,
@@ -405,10 +405,10 @@ mod tests {
405405
let a = A::One;
406406
let b = B::One;
407407
match <|>(a, b) {
408-
(A::One, B::One) => (),
409-
(A::One, B::Two) => (),
410-
(A::Two, B::One) => (),
411-
(A::Two, B::Two) => (),
408+
(A::One, B::One) => {}
409+
(A::One, B::Two) => {}
410+
(A::Two, B::One) => {}
411+
(A::Two, B::Two) => {}
412412
}
413413
}
414414
"#,
@@ -449,10 +449,10 @@ mod tests {
449449
let a = A::One;
450450
let b = B::One;
451451
match <|>(&a, &b) {
452-
(A::One, B::One) => (),
453-
(A::One, B::Two) => (),
454-
(A::Two, B::One) => (),
455-
(A::Two, B::Two) => (),
452+
(A::One, B::One) => {}
453+
(A::One, B::Two) => {}
454+
(A::Two, B::One) => {}
455+
(A::Two, B::Two) => {}
456456
}
457457
}
458458
"#,
@@ -477,7 +477,7 @@ mod tests {
477477
let a = A::One;
478478
let b = B::One;
479479
match (a<|>, b) {
480-
(A::Two, B::One) => (),
480+
(A::Two, B::One) => {}
481481
}
482482
}
483483
"#,
@@ -502,10 +502,10 @@ mod tests {
502502
let a = A::One;
503503
let b = B::One;
504504
match (a<|>, b) {
505-
(A::Two, B::One) => (),
506-
(A::One, B::One) => (),
507-
(A::One, B::Two) => (),
508-
(A::Two, B::Two) => (),
505+
(A::Two, B::One) => {}
506+
(A::One, B::One) => {}
507+
(A::One, B::Two) => {}
508+
(A::Two, B::Two) => {}
509509
}
510510
}
511511
"#,
@@ -555,7 +555,7 @@ mod tests {
555555
556556
fn foo(a: &A) {
557557
match <|>a {
558-
A::As => (),
558+
A::As => {}
559559
}
560560
}
561561
"#,
@@ -580,7 +580,7 @@ mod tests {
580580
581581
fn foo(a: &mut A) {
582582
match <|>a {
583-
A::Es { x, y } => (),
583+
A::Es { x, y } => {}
584584
}
585585
}
586586
"#,
@@ -611,7 +611,7 @@ mod tests {
611611
612612
fn main() {
613613
match E::X {
614-
<|>_ => {},
614+
<|>_ => {}
615615
}
616616
}
617617
"#,
@@ -620,8 +620,8 @@ mod tests {
620620
621621
fn main() {
622622
match <|>E::X {
623-
E::X => (),
624-
E::Y => (),
623+
E::X => {}
624+
E::Y => {}
625625
}
626626
}
627627
"#,
@@ -648,8 +648,8 @@ mod tests {
648648
649649
fn main() {
650650
match <|>X {
651-
X => (),
652-
foo::E::Y => (),
651+
X => {}
652+
foo::E::Y => {}
653653
}
654654
}
655655
"#,

crates/ra_syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub fn block_from_expr(e: ast::Expr) -> ast::Block {
8787
pub fn expr_unit() -> ast::Expr {
8888
expr_from_text("()")
8989
}
90+
pub fn expr_empty_block() -> ast::Expr {
91+
expr_from_text("{}")
92+
}
9093
pub fn expr_unimplemented() -> ast::Expr {
9194
expr_from_text("unimplemented!()")
9295
}

docs/user/assists.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ enum Action { Move { distance: u32 }, Stop }
267267

268268
fn handle(action: Action) {
269269
match action {
270-
Action::Move { distance } => (),
271-
Action::Stop => (),
270+
Action::Move { distance } => {}
271+
Action::Stop => {}
272272
}
273273
}
274274
```

0 commit comments

Comments
 (0)