Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e29fd7b

Browse files
committed
Only put { on a newline in a match arm where necessary
Fixes rust-lang#3005
1 parent 2f8c1fe commit e29fd7b

File tree

3 files changed

+58
-68
lines changed

3 files changed

+58
-68
lines changed

src/chains.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use rewrite::{Rewrite, RewriteContext};
7474
use shape::Shape;
7575
use source_map::SpanUtils;
7676
use utils::{
77-
first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident,
77+
self, first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident,
7878
trimmed_last_line_width, wrap_str,
7979
};
8080

@@ -130,7 +130,7 @@ enum ChainItemKind {
130130
impl ChainItemKind {
131131
fn is_block_like(&self, context: &RewriteContext, reps: &str) -> bool {
132132
match self {
133-
ChainItemKind::Parent(ref expr) => is_block_expr(context, expr, reps),
133+
ChainItemKind::Parent(ref expr) => utils::is_block_expr(context, expr, reps),
134134
ChainItemKind::MethodCall(..)
135135
| ChainItemKind::StructField(..)
136136
| ChainItemKind::TupleField(..)
@@ -845,38 +845,6 @@ impl<'a> ChainFormatter for ChainFormatterVisual<'a> {
845845
}
846846
}
847847

848-
// States whether an expression's last line exclusively consists of closing
849-
// parens, braces, and brackets in its idiomatic formatting.
850-
fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool {
851-
match expr.node {
852-
ast::ExprKind::Mac(..)
853-
| ast::ExprKind::Call(..)
854-
| ast::ExprKind::MethodCall(..)
855-
| ast::ExprKind::Array(..)
856-
| ast::ExprKind::Struct(..)
857-
| ast::ExprKind::While(..)
858-
| ast::ExprKind::WhileLet(..)
859-
| ast::ExprKind::If(..)
860-
| ast::ExprKind::IfLet(..)
861-
| ast::ExprKind::Block(..)
862-
| ast::ExprKind::Loop(..)
863-
| ast::ExprKind::ForLoop(..)
864-
| ast::ExprKind::Match(..) => repr.contains('\n'),
865-
ast::ExprKind::Paren(ref expr)
866-
| ast::ExprKind::Binary(_, _, ref expr)
867-
| ast::ExprKind::Index(_, ref expr)
868-
| ast::ExprKind::Unary(_, ref expr)
869-
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
870-
| ast::ExprKind::Try(ref expr)
871-
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
872-
// This can only be a string lit
873-
ast::ExprKind::Lit(_) => {
874-
repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()
875-
}
876-
_ => false,
877-
}
878-
}
879-
880848
/// Remove try operators (`?`s) that appear in the given string. If removing
881849
/// them leaves an empty line, remove that line as well unless it is the first
882850
/// line (we need the first newline for detecting pre/post comment).

src/matches.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -247,52 +247,42 @@ fn rewrite_match_arm(
247247
} else {
248248
(mk_sp(arm.span().lo(), arm.span().lo()), String::new())
249249
};
250-
let pats_str =
251-
rewrite_match_pattern(context, &ptr_vec_to_ref_vec(&arm.pats), &arm.guard, shape)
252-
.and_then(|pats_str| {
253-
combine_strs_with_missing_comments(
254-
context,
255-
&attrs_str,
256-
&pats_str,
257-
missing_span,
258-
shape,
259-
false,
260-
)
261-
})?;
262250

263-
let arrow_span = mk_sp(arm.pats.last().unwrap().span.hi(), arm.body.span.lo());
264-
rewrite_match_body(
265-
context,
266-
&arm.body,
267-
&pats_str,
268-
shape,
269-
arm.guard.is_some(),
270-
arrow_span,
271-
is_last,
272-
)
273-
}
274-
275-
fn rewrite_match_pattern(
276-
context: &RewriteContext,
277-
pats: &[&ast::Pat],
278-
guard: &Option<ast::Guard>,
279-
shape: Shape,
280-
) -> Option<String> {
281251
// Patterns
282252
// 5 = ` => {`
283253
let pat_shape = shape.sub_width(5)?;
284-
let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?;
254+
let pats_str = rewrite_multiple_patterns(context, &ptr_vec_to_ref_vec(&arm.pats), pat_shape)?;
285255

286256
// Guard
257+
let block_like_pat = trimmed_last_line_width(&pats_str) <= context.config.tab_spaces();
258+
let new_line_guard = pats_str.contains('\n') && !block_like_pat;
287259
let guard_str = rewrite_guard(
288260
context,
289-
guard,
261+
&arm.guard,
290262
shape,
291263
trimmed_last_line_width(&pats_str),
292-
pats_str.contains('\n'),
264+
new_line_guard,
293265
)?;
294266

295-
Some(format!("{}{}", pats_str, guard_str))
267+
let lhs_str = combine_strs_with_missing_comments(
268+
context,
269+
&attrs_str,
270+
&format!("{}{}", pats_str, guard_str),
271+
missing_span,
272+
shape,
273+
false,
274+
)?;
275+
276+
let arrow_span = mk_sp(arm.pats.last().unwrap().span.hi(), arm.body.span.lo());
277+
rewrite_match_body(
278+
context,
279+
&arm.body,
280+
&lhs_str,
281+
shape,
282+
guard_str.contains('\n'),
283+
arrow_span,
284+
is_last,
285+
)
296286
}
297287

298288
fn block_can_be_flattened<'a>(

src/utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,35 @@ pub fn starts_with_newline(s: &str) -> bool {
417417
pub fn first_line_ends_with(s: &str, c: char) -> bool {
418418
s.lines().next().map_or(false, |l| l.ends_with(c))
419419
}
420+
421+
// States whether an expression's last line exclusively consists of closing
422+
// parens, braces, and brackets in its idiomatic formatting.
423+
pub fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool {
424+
match expr.node {
425+
ast::ExprKind::Mac(..)
426+
| ast::ExprKind::Call(..)
427+
| ast::ExprKind::MethodCall(..)
428+
| ast::ExprKind::Array(..)
429+
| ast::ExprKind::Struct(..)
430+
| ast::ExprKind::While(..)
431+
| ast::ExprKind::WhileLet(..)
432+
| ast::ExprKind::If(..)
433+
| ast::ExprKind::IfLet(..)
434+
| ast::ExprKind::Block(..)
435+
| ast::ExprKind::Loop(..)
436+
| ast::ExprKind::ForLoop(..)
437+
| ast::ExprKind::Match(..) => repr.contains('\n'),
438+
ast::ExprKind::Paren(ref expr)
439+
| ast::ExprKind::Binary(_, _, ref expr)
440+
| ast::ExprKind::Index(_, ref expr)
441+
| ast::ExprKind::Unary(_, ref expr)
442+
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
443+
| ast::ExprKind::Try(ref expr)
444+
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
445+
// This can only be a string lit
446+
ast::ExprKind::Lit(_) => {
447+
repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()
448+
}
449+
_ => false,
450+
}
451+
}

0 commit comments

Comments
 (0)