diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index d5b34d98b2d..04251d09c74 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -118,9 +118,7 @@ fn execute() -> i32 { Operation::Stdin(input, write_mode) => { // try to read config from local directory let config = match lookup_and_read_project_file(&Path::new(".")) { - Ok((_, toml)) => { - Config::from_toml(&toml) - } + Ok((_, toml)) => Config::from_toml(&toml), Err(_) => Default::default(), }; diff --git a/src/chains.rs b/src/chains.rs index 31cd9624016..665fb5ac073 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -21,7 +21,7 @@ use Indent; use rewrite::{Rewrite, RewriteContext}; -use utils::first_line_width; +use utils::{wrap_str, first_line_width}; use expr::rewrite_call; use config::BlockIndentStyle; @@ -58,12 +58,8 @@ pub fn rewrite_chain(mut expr: &ast::Expr, } else { match context.config.chain_indent { BlockIndentStyle::Inherit => (context.block_indent, false), - BlockIndentStyle::Tabbed => { - (context.block_indent.block_indent(context.config), false) - } - BlockIndentStyle::Visual => { - (offset + Indent::new(context.config.tab_spaces, 0), false) - } + BlockIndentStyle::Tabbed => (context.block_indent.block_indent(context.config), false), + BlockIndentStyle::Visual => (offset + Indent::new(context.config.tab_spaces, 0), false), } }; @@ -142,10 +138,13 @@ pub fn rewrite_chain(mut expr: &ast::Expr, &connector[..] }; - Some(format!("{}{}{}", - parent_rewrite, - first_connector, - rewrites.join(&connector))) + wrap_str(format!("{}{}{}", + parent_rewrite, + first_connector, + rewrites.join(&connector)), + context.config.max_width, + width, + offset) } // States whether an expression's last line exclusively consists of closing @@ -171,13 +170,9 @@ fn is_block_expr(expr: &ast::Expr, repr: &str) -> bool { fn pop_expr_chain<'a>(expr: &'a ast::Expr) -> Option<&'a ast::Expr> { match expr.node { - ast::Expr_::ExprMethodCall(_, _, ref expressions) => { - Some(&expressions[0]) - } + ast::Expr_::ExprMethodCall(_, _, ref expressions) => Some(&expressions[0]), ast::Expr_::ExprTupField(ref subexpr, _) | - ast::Expr_::ExprField(ref subexpr, _) => { - Some(subexpr) - } + ast::Expr_::ExprField(ref subexpr, _) => Some(subexpr), _ => None, } } @@ -199,12 +194,8 @@ fn rewrite_chain_expr(expr: &ast::Expr, width, offset) } - ast::Expr_::ExprField(_, ref field) => { - Some(format!(".{}", field.node)) - } - ast::Expr_::ExprTupField(_, ref field) => { - Some(format!(".{}", field.node)) - } + ast::Expr_::ExprField(_, ref field) => Some(format!(".{}", field.node)), + ast::Expr_::ExprTupField(_, ref field) => Some(format!(".{}", field.node)), _ => unreachable!(), } } diff --git a/src/comment.rs b/src/comment.rs index eb6c5fc75ec..0f118ca5d29 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -127,12 +127,14 @@ impl FindUncommented for str { None => { return Some(i - pat.len()); } - Some(c) => match kind { - CodeCharKind::Normal if b == c => {} - _ => { - needle_iter = pat.chars(); + Some(c) => { + match kind { + CodeCharKind::Normal if b == c => {} + _ => { + needle_iter = pat.chars(); + } } - }, + } } } @@ -233,33 +235,39 @@ impl Iterator for CharClasses where T: Iterator, T::Item: RichChar { let item = try_opt!(self.base.next()); let chr = item.get_char(); self.status = match self.status { - CharClassesStatus::LitString => match chr { - '"' => CharClassesStatus::Normal, - '\\' => CharClassesStatus::LitStringEscape, - _ => CharClassesStatus::LitString, - }, + CharClassesStatus::LitString => { + match chr { + '"' => CharClassesStatus::Normal, + '\\' => CharClassesStatus::LitStringEscape, + _ => CharClassesStatus::LitString, + } + } CharClassesStatus::LitStringEscape => CharClassesStatus::LitString, - CharClassesStatus::LitChar => match chr { - '\\' => CharClassesStatus::LitCharEscape, - '\'' => CharClassesStatus::Normal, - _ => CharClassesStatus::LitChar, - }, + CharClassesStatus::LitChar => { + match chr { + '\\' => CharClassesStatus::LitCharEscape, + '\'' => CharClassesStatus::Normal, + _ => CharClassesStatus::LitChar, + } + } CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar, CharClassesStatus::Normal => { match chr { '"' => CharClassesStatus::LitString, '\'' => CharClassesStatus::LitChar, - '/' => match self.base.peek() { - Some(next) if next.get_char() == '*' => { - self.status = CharClassesStatus::BlockCommentOpening(1); - return Some((CodeCharKind::Comment, item)); - } - Some(next) if next.get_char() == '/' => { - self.status = CharClassesStatus::LineComment; - return Some((CodeCharKind::Comment, item)); + '/' => { + match self.base.peek() { + Some(next) if next.get_char() == '*' => { + self.status = CharClassesStatus::BlockCommentOpening(1); + return Some((CodeCharKind::Comment, item)); + } + Some(next) if next.get_char() == '/' => { + self.status = CharClassesStatus::LineComment; + return Some((CodeCharKind::Comment, item)); + } + _ => CharClassesStatus::Normal, } - _ => CharClassesStatus::Normal, - }, + } _ => CharClassesStatus::Normal, } } @@ -271,10 +279,12 @@ impl Iterator for CharClasses where T: Iterator, T::Item: RichChar { return Some((CodeCharKind::Comment, item)); } self.status = match self.base.peek() { - Some(next) if next.get_char() == '/' && chr == '*' => - CharClassesStatus::BlockCommentClosing(deepness - 1), - Some(next) if next.get_char() == '*' && chr == '/' => - CharClassesStatus::BlockCommentOpening(deepness + 1), + Some(next) if next.get_char() == '/' && chr == '*' => { + CharClassesStatus::BlockCommentClosing(deepness - 1) + } + Some(next) if next.get_char() == '*' && chr == '/' => { + CharClassesStatus::BlockCommentOpening(deepness + 1) + } _ => CharClassesStatus::BlockComment(deepness), }; return Some((CodeCharKind::Comment, item)); diff --git a/src/config.rs b/src/config.rs index 295c1076b5f..c02d618437f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -307,4 +307,5 @@ create_config! { take_source_hints: bool, true, "Retain some formatting characteristics from the source code"; hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment"; wrap_comments: bool, false, "Break comments to fit on the line"; + wrap_match_arms: bool, true, "Wrap multiline match arms in blocks"; } diff --git a/src/expr.rs b/src/expr.rs index 8b5fa9fa787..29d6d32c0dd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -58,9 +58,7 @@ impl Rewrite for ast::Expr { let inner_span = mk_sp(callee.span.hi, self.span.hi); rewrite_call(context, &**callee, args, inner_span, width, offset) } - ast::Expr_::ExprParen(ref subexpr) => { - rewrite_paren(context, subexpr, width, offset) - } + ast::Expr_::ExprParen(ref subexpr) => rewrite_paren(context, subexpr, width, offset), ast::Expr_::ExprBinary(ref op, ref lhs, ref rhs) => { rewrite_binary_op(context, op, lhs, rhs, width, offset) } @@ -91,9 +89,7 @@ impl Rewrite for ast::Expr { ast::Expr_::ExprLoop(ref block, label) => { Loop::new_loop(block, label).rewrite(context, width, offset) } - ast::Expr_::ExprBlock(ref block) => { - block.rewrite(context, width, offset) - } + ast::Expr_::ExprBlock(ref block) => block.rewrite(context, width, offset), ast::Expr_::ExprIf(ref cond, ref if_block, ref else_block) => { rewrite_if_else(context, cond, @@ -145,9 +141,7 @@ impl Rewrite for ast::Expr { } ast::Expr_::ExprField(..) | ast::Expr_::ExprTupField(..) | - ast::Expr_::ExprMethodCall(..) => { - rewrite_chain(self, context, width, offset) - } + ast::Expr_::ExprMethodCall(..) => rewrite_chain(self, context, width, offset), ast::Expr_::ExprMac(ref mac) => { // Failure to rewrite a marco should not imply failure to // rewrite the expression. @@ -571,13 +565,15 @@ impl<'a> Rewrite for Loop<'a> { let inner_offset = offset + self.keyword.len() + label_string.len(); let pat_expr_string = match self.cond { - Some(cond) => try_opt!(rewrite_pat_expr(context, - self.pat, - cond, - self.matcher, - self.connector, - inner_width, - inner_offset)), + Some(cond) => { + try_opt!(rewrite_pat_expr(context, + self.pat, + cond, + self.matcher, + self.connector, + inner_width, + inner_offset)) + } None => String::new(), }; @@ -711,6 +707,8 @@ fn block_contains_comment(block: &ast::Block, codemap: &CodeMap) -> bool { } // Checks that a block contains no statements, an expression and no comments. +// FIXME: incorrectly returns false when comment is contained completely within +// the expression. pub fn is_simple_block(block: &ast::Block, codemap: &CodeMap) -> bool { block.stmts.is_empty() && block.expr.is_some() && !block_contains_comment(block, codemap) } @@ -726,6 +724,14 @@ pub fn is_empty_block(block: &ast::Block, codemap: &CodeMap) -> bool { block.stmts.is_empty() && block.expr.is_none() && !block_contains_comment(block, codemap) } +fn is_unsafe_block(block: &ast::Block) -> bool { + if let ast::BlockCheckMode::UnsafeBlock(..) = block.rules { + true + } else { + false + } +} + // inter-match-arm-comment-rules: // - all comments following a match arm before the start of the next arm // are about the second arm @@ -907,7 +913,7 @@ impl Rewrite for ast::Arm { } let pats_width = if vertical { - pat_strs[pat_strs.len() - 1].len() + pat_strs.last().unwrap().len() } else { total_width }; @@ -934,65 +940,62 @@ impl Rewrite for ast::Arm { line_start += offset.width(); } + let body = match **body { + ast::Expr { node: ast::ExprBlock(ref block), .. } if !is_unsafe_block(block) && + is_simple_block(block, + context.codemap) && + context.config.wrap_match_arms => { + block.expr.as_ref().map(|e| &**e).unwrap() + } + ref x => x, + }; + let comma = arm_comma(body); // Let's try and get the arm body on the same line as the condition. // 4 = ` => `.len() - let same_line_body = if context.config.max_width > line_start + comma.len() + 4 { + if context.config.max_width > line_start + comma.len() + 4 { let budget = context.config.max_width - line_start - comma.len() - 4; let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent); let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget); match rewrite { - Some(ref body_str) if body_str.len() <= budget || comma.is_empty() => + Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms || + comma.is_empty() => { return Some(format!("{}{} => {}{}", attr_str.trim_left(), pats_str, body_str, - comma)), - _ => rewrite, + comma)); + } + _ => {} } - } else { - None - }; - - if let ast::ExprBlock(_) = body.node { - // We're trying to fit a block in, but it still failed, give up. - return None; } + // FIXME: we're doing a second rewrite of the expr; This may not be + // necessary. let body_budget = try_opt!(width.checked_sub(context.config.tab_spaces)); let indent = context.block_indent.block_indent(context.config); let inner_context = &RewriteContext { block_indent: indent, ..*context }; - let next_line_body = nop_block_collapse(body.rewrite(inner_context, body_budget, indent), - body_budget); - - let body_str = try_opt!(match_arm_heuristic(same_line_body.as_ref().map(|x| &x[..]), - next_line_body.as_ref().map(|x| &x[..]))); - - let spacer = match same_line_body { - Some(ref body) if body == body_str => " ".to_owned(), - _ => format!("\n{}", - offset.block_indent(context.config).to_string(context.config)), + let next_line_body = try_opt!(nop_block_collapse(body.rewrite(inner_context, + body_budget, + indent), + body_budget)); + let indent_str = offset.block_indent(context.config).to_string(context.config); + let (body_prefix, body_suffix) = if context.config.wrap_match_arms { + (" {", "}") + } else { + ("", "") }; - Some(format!("{}{} =>{}{},", + Some(format!("{}{} =>{}\n{}{}\n{}{}", attr_str.trim_left(), pats_str, - spacer, - body_str)) - } -} - -// Takes two possible rewrites for the match arm body and chooses the "nicest". -fn match_arm_heuristic<'a>(former: Option<&'a str>, latter: Option<&'a str>) -> Option<&'a str> { - match (former, latter) { - (f @ Some(..), None) => f, - (Some(f), Some(l)) if f.chars().filter(|&c| c == '\n').count() <= - l.chars().filter(|&c| c == '\n').count() => { - Some(f) - } - (_, l) => l, + body_prefix, + indent_str, + next_line_body, + offset.to_string(context.config), + body_suffix)) } } @@ -1285,9 +1288,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, // Foo { a: Foo } - indent is +3, width is -5. let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0); let (indent, v_budget) = match context.config.struct_lit_style { - StructLitStyle::Visual => { - (offset + path_str.len() + 3, h_budget) - } + StructLitStyle::Visual => (offset + path_str.len() + 3, h_budget), StructLitStyle::Block => { // If we are all on one line, then we'll ignore the indent, and we // have a smaller budget. @@ -1396,8 +1397,9 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, match (context.config.struct_lit_style, context.config.struct_lit_multiline_style) { - (StructLitStyle::Block, _) if fields_str.contains('\n') || fields_str.len() > h_budget => - format_on_newline(), + (StructLitStyle::Block, _) if fields_str.contains('\n') || fields_str.len() > h_budget => { + format_on_newline() + } (StructLitStyle::Block, MultilineStyle::ForceMulti) => format_on_newline(), _ => Some(format!("{} {{ {} }}", path_str, fields_str)), } diff --git a/src/imports.rs b/src/imports.rs index 6917b1ff403..42d152fc6ac 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -74,12 +74,8 @@ fn rewrite_single_use_list(path_str: String, vpi: &ast::PathListItem) -> String fn rewrite_path_item(vpi: &&ast::PathListItem) -> Option { let path_item_str = match vpi.node { - ast::PathListItem_::PathListIdent{ name, .. } => { - name.to_string() - } - ast::PathListItem_::PathListMod{ .. } => { - "self".to_owned() - } + ast::PathListItem_::PathListIdent{ name, .. } => name.to_string(), + ast::PathListItem_::PathListMod{ .. } => "self".to_owned(), }; Some(append_alias(path_item_str, vpi)) diff --git a/src/items.rs b/src/items.rs index c7cfc732fc1..19aec73f116 100644 --- a/src/items.rs +++ b/src/items.rs @@ -564,23 +564,26 @@ impl<'a> FmtVisitor<'a> { ")", |arg| { match *arg { - ArgumentKind::Regular(arg) => - span_lo_for_arg(arg), + ArgumentKind::Regular(arg) => { + span_lo_for_arg(arg) + } ArgumentKind::Variadic(start) => start, } }, |arg| { match *arg { ArgumentKind::Regular(arg) => arg.ty.span.hi, - ArgumentKind::Variadic(start) => - start + BytePos(3), + ArgumentKind::Variadic(start) => { + start + BytePos(3) + } } }, |arg| { match *arg { ArgumentKind::Regular(..) => None, - ArgumentKind::Variadic(..) => - Some("...".to_owned()), + ArgumentKind::Variadic(..) => { + Some("...".to_owned()) + } } }, comment_span_start, @@ -821,9 +824,7 @@ impl<'a> FmtVisitor<'a> { offset: Indent) -> Option { match *struct_def { - ast::VariantData::Unit(..) => { - self.format_unit_struct(item_name, ident, vis) - } + ast::VariantData::Unit(..) => self.format_unit_struct(item_name, ident, vis), ast::VariantData::Tuple(ref fields, _) => { self.format_tuple_struct(item_name, ident, vis, fields, generics, span, offset) } @@ -874,12 +875,14 @@ impl<'a> FmtVisitor<'a> { offset + header_str.len(), mk_sp(span.lo, body_lo))) } - None => if self.config.item_brace_style == BraceStyle::AlwaysNextLine && - !fields.is_empty() { - format!("\n{}{{", self.block_indent.to_string(self.config)) - } else { - " {".to_owned() - }, + None => { + if self.config.item_brace_style == BraceStyle::AlwaysNextLine && + !fields.is_empty() { + format!("\n{}{{", self.block_indent.to_string(self.config)) + } else { + " {".to_owned() + } + } }; result.push_str(&generics_str); @@ -1118,8 +1121,9 @@ impl<'a> FmtVisitor<'a> { let extra_indent = match self.config.where_indent { BlockIndentStyle::Inherit => Indent::empty(), - BlockIndentStyle::Tabbed | BlockIndentStyle::Visual => - Indent::new(config.tab_spaces, 0), + BlockIndentStyle::Tabbed | BlockIndentStyle::Visual => { + Indent::new(config.tab_spaces, 0) + } }; let context = self.get_context(); diff --git a/src/lib.rs b/src/lib.rs index bf1a0164f68..3923f6b742d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -225,15 +225,9 @@ pub enum ErrorKind { impl fmt::Display for ErrorKind { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { - ErrorKind::LineOverflow => { - write!(fmt, "line exceeded maximum length") - } - ErrorKind::TrailingWhitespace => { - write!(fmt, "left behind trailing whitespace") - } - ErrorKind::BadIssue(issue) => { - write!(fmt, "found {}", issue) - } + ErrorKind::LineOverflow => write!(fmt, "line exceeded maximum length"), + ErrorKind::TrailingWhitespace => write!(fmt, "left behind trailing whitespace"), + ErrorKind::BadIssue(issue) => write!(fmt, "found {}", issue), } } } diff --git a/src/lists.rs b/src/lists.rs index b05e4d46066..ecde4f19fe4 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -392,9 +392,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3> match (block_open_index, newline_index) { // Separator before comment, with the next item on same line. // Comment belongs to next item. - (Some(i), None) if i > separator_index => { - separator_index + 1 - } + (Some(i), None) if i > separator_index => separator_index + 1, // Block-style post-comment before the separator. (Some(i), None) => { cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, diff --git a/src/missed_spans.rs b/src/missed_spans.rs index b83f07e05d7..727bc49a742 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -103,10 +103,12 @@ impl<'a> FmtVisitor<'a> { } let replaced = match self.write_mode { - Some(mode) => match mode { - WriteMode::Coverage => replace_chars(old_snippet), - _ => old_snippet.to_owned(), - }, + Some(mode) => { + match mode { + WriteMode::Coverage => replace_chars(old_snippet), + _ => old_snippet.to_owned(), + } + } None => old_snippet.to_owned(), }; let snippet = &*replaced; diff --git a/src/patterns.rs b/src/patterns.rs index 3e7a11bad41..af0bd827751 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -21,9 +21,7 @@ use syntax::ast::{BindingMode, Pat, Pat_}; impl Rewrite for Pat { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option { match self.node { - Pat_::PatBox(ref pat) => { - rewrite_unary_prefix(context, "box ", &**pat, width, offset) - } + Pat_::PatBox(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset), Pat_::PatIdent(binding_mode, ident, None) => { let (prefix, mutability) = match binding_mode { BindingMode::BindByRef(mutability) => ("ref ", mutability), @@ -50,9 +48,7 @@ impl Rewrite for Pat { let prefix = format!("&{}", format_mutability(mutability)); rewrite_unary_prefix(context, &prefix, &**pat, width, offset) } - Pat_::PatTup(ref items) => { - rewrite_tuple(context, items, self.span, width, offset) - } + Pat_::PatTup(ref items) => rewrite_tuple(context, items, self.span, width, offset), Pat_::PatEnum(ref path, Some(ref pat_vec)) => { let path_str = try_opt!(::types::rewrite_path(context, true, diff --git a/src/types.rs b/src/types.rs index 9b50333e043..9e693e9cff6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -137,9 +137,7 @@ impl<'a> Rewrite for SegmentParam<'a> { width, offset) } - SegmentParam::Type(ref ty) => { - ty.rewrite(context, width, offset) - } + SegmentParam::Type(ref ty) => ty.rewrite(context, width, offset), SegmentParam::Binding(ref binding) => { let mut result = format!("{} = ", binding.ident); let budget = try_opt!(width.checked_sub(result.len())); @@ -479,9 +477,7 @@ impl Rewrite for ast::Ty { let budget = try_opt!(width.checked_sub(2)); ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str)) } - ast::TyTup(ref items) => { - rewrite_tuple(context, items, self.span, width, offset) - } + ast::TyTup(ref items) => rewrite_tuple(context, items, self.span, width, offset), ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset), ast::TyPath(ref q_self, ref path) => { rewrite_path(context, false, q_self.as_ref(), path, width, offset) diff --git a/src/utils.rs b/src/utils.rs index 3965fc93c57..37dfb756972 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -75,9 +75,7 @@ pub fn last_line_width(s: &str) -> usize { fn is_skip(meta_item: &MetaItem) -> bool { match meta_item.node { MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION, - MetaItem_::MetaList(ref s, ref l) => { - *s == "cfg_attr" && l.len() == 2 && is_skip(&l[1]) - } + MetaItem_::MetaList(ref s, ref l) => *s == "cfg_attr" && l.len() == 2 && is_skip(&l[1]), _ => false, } } diff --git a/tests/source/match-nowrap.rs b/tests/source/match-nowrap.rs new file mode 100644 index 00000000000..83e0fbbd450 --- /dev/null +++ b/tests/source/match-nowrap.rs @@ -0,0 +1,12 @@ +// rustfmt-wrap_match_arms: false +// Match expressions, no unwrapping of block arms or wrapping of multiline +// expressions. + +fn foo() { + match x { + a => { foo() } + b => + (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb), + } +} diff --git a/tests/source/match.rs b/tests/source/match.rs index 9277319295e..b4d35cb3815 100644 --- a/tests/source/match.rs +++ b/tests/source/match.rs @@ -11,7 +11,6 @@ fn foo() { an_expression; foo() } - // Perhaps this should introduce braces? Foo(ref bar) => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, Pattern1 | Pattern2 | Pattern3 => false, @@ -83,7 +82,11 @@ fn main() { "scopeid"), true, true), - } + }; + + match x{ + y=>{/*Block with comment. Preserve me.*/ } + z=>{stmt();} } } fn matches() { diff --git a/tests/system.rs b/tests/system.rs index 4a924c57833..6d3c817256c 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -249,8 +249,10 @@ fn handle_result(result: HashMap, fn get_target(file_name: &str, target: Option<&str>, write_mode: WriteMode) -> String { let file_path = Path::new(file_name); let (source_path_prefix, target_path_prefix) = match write_mode { - WriteMode::Coverage => (Path::new("tests/coverage-source/"), - "tests/coverage-target/"), + WriteMode::Coverage => { + (Path::new("tests/coverage-source/"), + "tests/coverage-target/") + } _ => (Path::new("tests/source/"), "tests/target/"), }; diff --git a/tests/target/match-nowrap.rs b/tests/target/match-nowrap.rs new file mode 100644 index 00000000000..db2a874c838 --- /dev/null +++ b/tests/target/match-nowrap.rs @@ -0,0 +1,13 @@ +// rustfmt-wrap_match_arms: false +// Match expressions, no unwrapping of block arms or wrapping of multiline +// expressions. + +fn foo() { + match x { + a => { + foo() + } + b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb), + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index 1d6842d358a..2c238ea8a68 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -12,14 +12,12 @@ fn foo() { an_expression; foo() } - // Perhaps this should introduce braces? - Foo(ref bar) => - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + Foo(ref bar) => { + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + } Pattern1 | Pattern2 | Pattern3 => false, Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn | - Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => { - blah - } + Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => blah, Patternnnnnnnnnnnnnnnnnnn | Patternnnnnnnnnnnnnnnnnnn | Patternnnnnnnnnnnnnnnnnnn | @@ -29,8 +27,9 @@ fn foo() { Patternnnnnnnnnnnnnnnnnnn if looooooooooooooooooong_guard => meh, Patternnnnnnnnnnnnnnnnnnnnnnnnn | - Patternnnnnnnnnnnnnnnnnnnnnnnnn if looooooooooooooooooooooooooooooooooooooooong_guard => - meh, + Patternnnnnnnnnnnnnnnnnnnnnnnnn if looooooooooooooooooooooooooooooooooooooooong_guard => { + meh + } // Test that earlier patterns can take the guard space (aaaa, @@ -75,19 +74,33 @@ fn main() { // Test that one-line bodies align. fn main() { match r { - Variableeeeeeeeeeeeeeeeee => ("variable", - vec!["id", "name", "qualname", "value", "type", "scopeid"], - true, - true), - Enummmmmmmmmmmmmmmmmmmmm => ("enum", - vec!["id", "qualname", "scopeid", "value"], - true, - true), - Variantttttttttttttttttttttttt => + Variableeeeeeeeeeeeeeeeee => { + ("variable", + vec!["id", "name", "qualname", "value", "type", "scopeid"], + true, + true) + } + Enummmmmmmmmmmmmmmmmmmmm => { + ("enum", + vec!["id", "qualname", "scopeid", "value"], + true, + true) + } + Variantttttttttttttttttttttttt => { ("variant", vec!["id", "name", "qualname", "type", "value", "scopeid"], true, - true), + true) + } + }; + + match x { + y => { + // Block with comment. Preserve me. + } + z => { + stmt(); + } } } @@ -157,15 +170,9 @@ fn issue355() { a => println!("a", b), b => vec![1, 2], c => vec!(3; 4), - d => { - println!("a", b) - } - e => { - vec![1, 2] - } - f => { - vec!(3; 4) - } + d => println!("a", b), + e => vec![1, 2], + f => vec!(3; 4), h => println!("a", b), // h comment i => vec![1, 2], // i comment j => vec!(3; 4), // j comment @@ -176,22 +183,28 @@ fn issue355() { // m comment m => vec!(3; 4), // Rewrite splits macro - nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => - println!("a", b), + nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => { + println!("a", b) + } // Rewrite splits macro - oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => - vec![1, 2], + oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo => { + vec![1, 2] + } // Macro support fails to recognise this macro as splitable // We push the whole expr to a new line, TODO split this macro as well - pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => - vec!(3; 4), + pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp => { + vec!(3; 4) + } // q, r and s: Rewrite splits match arm - qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => - println!("a", b), - rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => - vec![1, 2], - ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => - vec!(3; 4), + qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq => { + println!("a", b) + } + rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr => { + vec![1, 2] + } + ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss => { + vec!(3; 4) + } // Funky bracketing styles t => println!{"a", b}, u => vec![1, 2], @@ -206,20 +219,24 @@ fn issue355() { wc => println!["a", b], // comment xc => vec![1, 2], // comment yc => vec![3; 4], // comment - yd => looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(aaaaaaaaaa, - bbbbbbbbbb, - cccccccccc, - dddddddddd), + yd => { + looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(aaaaaaaaaa, + bbbbbbbbbb, + cccccccccc, + dddddddddd) + } } } fn issue280() { { match x { - CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => - ch == '\n', - ast::ItemConst(ref typ, ref expr) => - self.process_static_or_const_item(item, &typ, &expr), + CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => { + ch == '\n' + } + ast::ItemConst(ref typ, ref expr) => { + self.process_static_or_const_item(item, &typ, &expr) + } } } } @@ -253,12 +270,11 @@ fn issue496() { { { match def { - def::DefConst(def_id) | def::DefAssociatedConst(def_id) => + def::DefConst(def_id) | def::DefAssociatedConst(def_id) => { match const_eval::lookup_const_by_id(cx.tcx, def_id, Some(self.pat.id)) { - Some(const_expr) => { - x - } - }, + Some(const_expr) => x, + } + } } } } @@ -268,14 +284,15 @@ fn issue496() { fn issue494() { { match stmt.node { - hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => + hir::StmtExpr(ref expr, id) | hir::StmtSemi(ref expr, id) => { result.push(StmtRef::Mirror(Box::new(Stmt { span: stmt.span, kind: StmtKind::Expr { scope: cx.tcx.region_maps.node_extent(id), expr: expr.to_ref(), }, - }))), + }))) + } } } }