1 //! Format match expression.
5 use rustc_ast::{ast, ptr};
6 use rustc_span::{BytePos, Span};
8 use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
9 use crate::config::lists::*;
10 use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version};
12 format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond,
15 use crate::lists::{itemize_list, write_list, ListFormatting};
16 use crate::rewrite::{Rewrite, RewriteContext};
17 use crate::shape::Shape;
18 use crate::source_map::SpanUtils;
19 use crate::spanned::Spanned;
21 contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
22 semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
25 /// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
26 struct ArmWrapper<'a> {
28 /// `true` if the arm is the last one in match expression. Used to decide on whether we should
29 /// add trailing comma to the match arm when `config.trailing_comma() == Never`.
31 /// Holds a byte position of `|` at the beginning of the arm pattern, if available.
32 beginning_vert: Option<BytePos>,
35 impl<'a> ArmWrapper<'a> {
36 fn new(arm: &'a ast::Arm, is_last: bool, beginning_vert: Option<BytePos>) -> ArmWrapper<'a> {
45 impl<'a> Spanned for ArmWrapper<'a> {
46 fn span(&self) -> Span {
47 if let Some(lo) = self.beginning_vert {
48 let lo = std::cmp::min(lo, self.arm.span().lo());
49 mk_sp(lo, self.arm.span().hi())
56 impl<'a> Rewrite for ArmWrapper<'a> {
57 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
63 self.beginning_vert.is_some(),
68 pub(crate) fn rewrite_match(
69 context: &RewriteContext<'_>,
74 attrs: &[ast::Attribute],
76 // Do not take the rhs overhead from the upper expressions into account
77 // when rewriting match condition.
78 let cond_shape = Shape {
79 width: context.budget(shape.used_width()),
83 let cond_shape = match context.config.indent_style() {
84 IndentStyle::Visual => cond_shape.shrink_left(6)?,
85 IndentStyle::Block => cond_shape.offset_left(6)?,
87 let cond_str = cond.rewrite(context, cond_shape)?;
88 let alt_block_sep = &shape.indent.to_string_with_newline(context.config);
89 let block_sep = match context.config.control_brace_style() {
90 ControlBraceStyle::AlwaysNextLine => alt_block_sep,
91 _ if last_line_extendable(&cond_str) => " ",
93 _ if cond_str.contains('\n') || cond_str.len() + 2 > cond_shape.width => alt_block_sep,
97 let nested_indent_str = shape
99 .block_indent(context.config)
100 .to_string(context.config);
102 let inner_attrs = &inner_attributes(attrs);
103 let inner_attrs_str = if inner_attrs.is_empty() {
107 .rewrite(context, shape)
108 .map(|s| format!("{}{}\n", nested_indent_str, s))?
111 let open_brace_pos = if inner_attrs.is_empty() {
112 let hi = if arms.is_empty() {
119 .span_after(mk_sp(cond.span.hi(), hi), "{")
121 inner_attrs[inner_attrs.len() - 1].span.hi()
125 let snippet = context.snippet(mk_sp(open_brace_pos, span.hi() - BytePos(1)));
126 if snippet.trim().is_empty() {
127 Some(format!("match {} {{}}", cond_str))
129 // Empty match with comments or inner attributes? We are not going to bother, sorry ;)
130 Some(context.snippet(span).to_owned())
133 let span_after_cond = mk_sp(cond.span.hi(), span.hi());
135 "match {}{}{{\n{}{}{}\n{}}}",
140 rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
141 shape.indent.to_string(context.config),
146 fn arm_comma(config: &Config, body: &ast::Expr, is_last: bool) -> &'static str {
147 if is_last && config.trailing_comma() == SeparatorTactic::Never {
149 } else if config.match_block_trailing_comma() {
151 } else if let ast::ExprKind::Block(ref block, _) = body.kind {
152 if let ast::BlockCheckMode::Default = block.rules {
162 /// Collect a byte position of the beginning `|` for each arm, if available.
163 fn collect_beginning_verts(
164 context: &RewriteContext<'_>,
166 ) -> Vec<Option<BytePos>> {
172 .then(|| a.pat.span().lo())
177 fn rewrite_match_arms(
178 context: &RewriteContext<'_>,
182 open_brace_pos: BytePos,
183 ) -> Option<String> {
184 let arm_shape = shape
185 .block_indent(context.config.tab_spaces())
186 .with_max_width(context.config);
188 let arm_len = arms.len();
189 let is_last_iter = repeat(false)
190 .take(arm_len.saturating_sub(1))
191 .chain(repeat(true));
192 let beginning_verts = collect_beginning_verts(context, arms);
193 let items = itemize_list(
194 context.snippet_provider,
197 .zip(beginning_verts.into_iter())
198 .map(|((arm, is_last), beginning_vert)| ArmWrapper::new(arm, is_last, beginning_vert)),
201 |arm| arm.span().lo(),
202 |arm| arm.span().hi(),
203 |arm| arm.rewrite(context, arm_shape),
208 let arms_vec: Vec<_> = items.collect();
209 // We will add/remove commas inside `arm.rewrite()`, and hence no separator here.
210 let fmt = ListFormatting::new(arm_shape, context.config)
212 .preserve_newline(true);
214 write_list(&arms_vec, &fmt)
217 fn rewrite_match_arm(
218 context: &RewriteContext<'_>,
222 has_leading_pipe: bool,
223 ) -> Option<String> {
224 let (missing_span, attrs_str) = if !arm.attrs.is_empty() {
225 if contains_skip(&arm.attrs) {
226 let (_, body) = flatten_arm_body(context, &arm.body, None);
227 // `arm.span()` does not include trailing comma, add it manually.
230 context.snippet(arm.span()),
231 arm_comma(context.config, body, is_last),
234 let missing_span = mk_sp(arm.attrs[arm.attrs.len() - 1].span.hi(), arm.pat.span.lo());
235 (missing_span, arm.attrs.rewrite(context, shape)?)
237 (mk_sp(arm.span().lo(), arm.span().lo()), String::new())
240 // Leading pipe offset
242 let (pipe_offset, pipe_str) = match context.config.match_arm_leading_pipes() {
243 MatchArmLeadingPipe::Never => (0, ""),
244 MatchArmLeadingPipe::Preserve if !has_leading_pipe => (0, ""),
245 MatchArmLeadingPipe::Preserve | MatchArmLeadingPipe::Always => (2, "| "),
250 let pat_shape = shape.sub_width(5)?.offset_left(pipe_offset)?;
251 let pats_str = arm.pat.rewrite(context, pat_shape)?;
254 let block_like_pat = trimmed_last_line_width(&pats_str) <= context.config.tab_spaces();
255 let new_line_guard = pats_str.contains('\n') && !block_like_pat;
256 let guard_str = rewrite_guard(
260 trimmed_last_line_width(&pats_str),
264 let lhs_str = combine_strs_with_missing_comments(
267 &format!("{}{}{}", pipe_str, pats_str, guard_str),
273 let arrow_span = mk_sp(arm.pat.span.hi(), arm.body.span().lo());
279 guard_str.contains('\n'),
285 fn stmt_is_expr_mac(stmt: &ast::Stmt) -> bool {
286 if let ast::StmtKind::Expr(expr) = &stmt.kind {
287 if let ast::ExprKind::MacCall(_) = &expr.kind {
294 fn block_can_be_flattened<'a>(
295 context: &RewriteContext<'_>,
297 ) -> Option<&'a ast::Block> {
299 ast::ExprKind::Block(ref block, _)
300 if !is_unsafe_block(block)
301 && !context.inside_macro()
302 && is_simple_block(context, block, Some(&expr.attrs))
303 && !stmt_is_expr_mac(&block.stmts[0]) =>
312 // @extend: true if the arm body can be put next to `=>`
313 // @body: flattened body, if the body is block with a single expression
314 fn flatten_arm_body<'a>(
315 context: &'a RewriteContext<'_>,
317 opt_shape: Option<Shape>,
318 ) -> (bool, &'a ast::Expr) {
320 |expr| !context.config.force_multiline_blocks() && can_flatten_block_around_this(expr);
322 if let Some(block) = block_can_be_flattened(context, body) {
323 if let ast::StmtKind::Expr(ref expr) = block.stmts[0].kind {
324 if let ast::ExprKind::Block(..) = expr.kind {
325 if expr.attrs.is_empty() {
326 flatten_arm_body(context, expr, None)
331 let cond_becomes_muti_line = opt_shape
332 .and_then(|shape| rewrite_cond(context, expr, shape))
333 .map_or(false, |cond| cond.contains('\n'));
334 if cond_becomes_muti_line {
337 (can_extend(expr), &*expr)
344 (can_extend(body), &*body)
348 fn rewrite_match_body(
349 context: &RewriteContext<'_>,
350 body: &ptr::P<ast::Expr>,
356 ) -> Option<String> {
357 let (extend, body) = flatten_arm_body(
360 shape.offset_left(extra_offset(pats_str, shape) + 4),
362 let (is_block, is_empty_block) = if let ast::ExprKind::Block(ref block, _) = body.kind {
363 (true, is_empty_block(context, block, Some(&body.attrs)))
368 let comma = arm_comma(context.config, body, is_last);
369 let alt_block_sep = &shape.indent.to_string_with_newline(context.config);
371 let combine_orig_body = |body_str: &str| {
372 let block_sep = match context.config.control_brace_style() {
373 ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep,
377 Some(format!("{} =>{}{}{}", pats_str, block_sep, body_str, comma))
380 let next_line_indent = if !is_block || is_empty_block {
381 shape.indent.block_indent(context.config)
386 let forbid_same_line =
387 (has_guard && pats_str.contains('\n') && !is_empty_block) || !body.attrs.is_empty();
389 // Look for comments between `=>` and the start of the body.
390 let arrow_comment = {
391 let arrow_snippet = context.snippet(arrow_span).trim();
392 // search for the arrow starting from the end of the snippet since there may be a match
393 // expression within the guard
394 let arrow_index = arrow_snippet.rfind("=>").unwrap();
396 let comment_str = arrow_snippet[arrow_index + 2..].trim();
397 if comment_str.is_empty() {
400 rewrite_comment(comment_str, false, shape, context.config)?
404 let combine_next_line_body = |body_str: &str| {
405 let nested_indent_str = next_line_indent.to_string_with_newline(context.config);
408 let mut result = pats_str.to_owned();
409 result.push_str(" =>");
410 if !arrow_comment.is_empty() {
411 result.push_str(&nested_indent_str);
412 result.push_str(&arrow_comment);
414 result.push_str(&nested_indent_str);
415 result.push_str(body_str);
416 result.push_str(comma);
420 let indent_str = shape.indent.to_string_with_newline(context.config);
421 let (body_prefix, body_suffix) =
422 if context.config.match_arm_blocks() && !context.inside_macro() {
423 let comma = if context.config.match_block_trailing_comma() {
428 let semicolon = if context.config.version() == Version::One {
431 if semicolon_for_expr(context, body) {
437 ("{", format!("{}{}}}{}", semicolon, indent_str, comma))
439 ("", String::from(","))
442 let block_sep = match context.config.control_brace_style() {
443 ControlBraceStyle::AlwaysNextLine => format!("{}{}", alt_block_sep, body_prefix),
444 _ if body_prefix.is_empty() => "".to_owned(),
445 _ if forbid_same_line || !arrow_comment.is_empty() => {
446 format!("{}{}", alt_block_sep, body_prefix)
448 _ => format!(" {}", body_prefix),
449 } + &nested_indent_str;
451 let mut result = pats_str.to_owned();
452 result.push_str(" =>");
453 if !arrow_comment.is_empty() {
454 result.push_str(&indent_str);
455 result.push_str(&arrow_comment);
457 result.push_str(&block_sep);
458 result.push_str(body_str);
459 result.push_str(&body_suffix);
463 // Let's try and get the arm body on the same line as the condition.
465 let orig_body_shape = shape
466 .offset_left(extra_offset(pats_str, shape) + 4)
467 .and_then(|shape| shape.sub_width(comma.len()));
468 let orig_body = if forbid_same_line || !arrow_comment.is_empty() {
470 } else if let Some(body_shape) = orig_body_shape {
471 let rewrite = nop_block_collapse(
472 format_expr(body, ExprType::Statement, context, body_shape),
479 || (!body_str.contains('\n')
480 && unicode_str_width(body_str) <= body_shape.width) =>
482 return combine_orig_body(body_str);
489 let orig_budget = orig_body_shape.map_or(0, |shape| shape.width);
491 // Try putting body on the next line and see if it looks better.
492 let next_line_body_shape = Shape::indented(next_line_indent, context.config);
493 let next_line_body = nop_block_collapse(
494 format_expr(body, ExprType::Statement, context, next_line_body_shape),
495 next_line_body_shape.width,
497 match (orig_body, next_line_body) {
498 (Some(ref orig_str), Some(ref next_line_str))
499 if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>
501 combine_next_line_body(next_line_str)
503 (Some(ref orig_str), _) if extend && first_line_width(orig_str) <= orig_budget => {
504 combine_orig_body(orig_str)
506 (Some(ref orig_str), Some(ref next_line_str)) if orig_str.contains('\n') => {
507 combine_next_line_body(next_line_str)
509 (None, Some(ref next_line_str)) => combine_next_line_body(next_line_str),
510 (None, None) => None,
511 (Some(ref orig_str), _) => combine_orig_body(orig_str),
515 // The `if ...` guard on a match arm.
517 context: &RewriteContext<'_>,
518 guard: &Option<ptr::P<ast::Expr>>,
520 // The amount of space used up on this line for the pattern in
521 // the arm (excludes offset).
522 pattern_width: usize,
523 multiline_pattern: bool,
524 ) -> Option<String> {
525 if let Some(ref guard) = *guard {
526 // First try to fit the guard string on the same line as the pattern.
527 // 4 = ` if `, 5 = ` => {`
528 let cond_shape = shape
529 .offset_left(pattern_width + 4)
530 .and_then(|s| s.sub_width(5));
531 if !multiline_pattern {
532 if let Some(cond_shape) = cond_shape {
533 if let Some(cond_str) = guard.rewrite(context, cond_shape) {
534 if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() {
535 return Some(format!(" if {}", cond_str));
541 // Not enough space to put the guard after the pattern, try a newline.
542 // 3 = `if `, 5 = ` => {`
543 let cond_shape = Shape::indented(shape.indent.block_indent(context.config), context.config)
545 .and_then(|s| s.sub_width(5));
546 if let Some(cond_shape) = cond_shape {
547 if let Some(cond_str) = guard.rewrite(context, cond_shape) {
550 cond_shape.indent.to_string_with_newline(context.config),
562 fn nop_block_collapse(block_str: Option<String>, budget: usize) -> Option<String> {
563 debug!("nop_block_collapse {:?} {}", block_str, budget);
564 block_str.map(|block_str| {
565 if block_str.starts_with('{')
567 && (block_str[1..].find(|c: char| !c.is_whitespace()).unwrap() == block_str.len() - 2)
576 fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
578 // We do not allow `if` to stay on the same line, since we could easily mistake
579 // `pat => if cond { ... }` and `pat if cond => { ... }`.
580 ast::ExprKind::If(..) => false,
581 // We do not allow collapsing a block around expression with condition
582 // to avoid it being cluttered with match arm.
583 ast::ExprKind::ForLoop(..) | ast::ExprKind::While(..) => false,
584 ast::ExprKind::Loop(..)
585 | ast::ExprKind::Match(..)
586 | ast::ExprKind::Block(..)
587 | ast::ExprKind::Closure(..)
588 | ast::ExprKind::Array(..)
589 | ast::ExprKind::Call(..)
590 | ast::ExprKind::MethodCall(..)
591 | ast::ExprKind::MacCall(..)
592 | ast::ExprKind::Struct(..)
593 | ast::ExprKind::Tup(..) => true,
594 ast::ExprKind::AddrOf(_, _, ref expr)
595 | ast::ExprKind::Box(ref expr)
596 | ast::ExprKind::Try(ref expr)
597 | ast::ExprKind::Unary(_, ref expr)
598 | ast::ExprKind::Index(ref expr, _)
599 | ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr),