From d8aeabaee103bc1788ee237c6bdcd4c345923963 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Thu, 26 Jan 2023 13:50:02 -0500 Subject: [PATCH] include block label length when calculating pat_shape of a match arm Previously we alwasy assumed the match arm pattern would have `shape.width` - 5 characters of space to work with. Now if we're formatting a block expression with a label we'll take the label into account. --- src/matches.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index aac5e59b860..fe9e7836ba7 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -246,8 +246,18 @@ fn rewrite_match_arm( }; // Patterns - // 5 = ` => {` - let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?; + let pat_shape = match &arm.body.kind { + ast::ExprKind::Block(_, Some(label)) => { + // Some block with a label ` => 'label: {` + // 7 = ` => : {` + let label_len = label.ident.as_str().len(); + shape.sub_width(7 + label_len)?.offset_left(pipe_offset)? + } + _ => { + // 5 = ` => {` + shape.sub_width(5)?.offset_left(pipe_offset)? + } + }; let pats_str = arm.pat.rewrite(context, pat_shape)?; // Guard