From 98c0570a284c477285e59bb1928b582e7089a597 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sun, 29 May 2016 17:58:38 +0200 Subject: [PATCH] Format non-statement if-else expressions on a single line --- src/chains.rs | 20 +- src/config.rs | 4 +- src/expr.rs | 406 +++++++++--------- src/imports.rs | 12 +- src/items.rs | 18 +- src/lists.rs | 6 +- src/missed_spans.rs | 8 +- src/patterns.rs | 6 +- src/types.rs | 6 +- src/utils.rs | 6 +- src/visitor.rs | 6 +- tests/config/small_tabs.toml | 2 +- tests/source/chains-block-indented-base.rs | 1 + tests/source/chains-no-overflow.rs | 1 + tests/source/chains-visual.rs | 1 + tests/source/chains.rs | 1 + tests/source/closure.rs | 6 +- .../else-if-brace-style-always-next-line.rs | 1 + .../else-if-brace-style-always-same-line.rs | 1 + .../else-if-brace-style-closing-next-line.rs | 1 + tests/source/expr.rs | 17 + tests/source/hard-tabs.rs | 1 + tests/source/single-line-if-else.rs | 2 +- tests/target/chains-block-indented-base.rs | 1 + tests/target/chains-no-overflow.rs | 1 + tests/target/chains-visual.rs | 1 + tests/target/chains.rs | 1 + tests/target/closure.rs | 10 +- .../else-if-brace-style-always-next-line.rs | 1 + .../else-if-brace-style-always-same-line.rs | 1 + .../else-if-brace-style-closing-next-line.rs | 1 + tests/target/expr.rs | 18 +- tests/target/hard-tabs.rs | 1 + tests/target/single-line-if-else.rs | 2 +- 34 files changed, 274 insertions(+), 297 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index faf368d41c1..6b8c4e50246 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -354,27 +354,13 @@ fn rewrite_chain_subexpr(expr: &ast::Expr, } ast::ExprKind::Field(_, ref field) => { let s = format!(".{}", field.node); - if s.len() <= width { - Some(s) - } else { - None - } + if s.len() <= width { Some(s) } else { None } } ast::ExprKind::TupField(_, ref field) => { let s = format!(".{}", field.node); - if s.len() <= width { - Some(s) - } else { - None - } - } - ast::ExprKind::Try(_) => { - if width >= 1 { - Some("?".into()) - } else { - None - } + if s.len() <= width { Some(s) } else { None } } + ast::ExprKind::Try(_) => if width >= 1 { Some("?".into()) } else { None }, _ => unreachable!(), } } diff --git a/src/config.rs b/src/config.rs index e4d0e4517c2..8a51b3ecefa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -381,7 +381,9 @@ create_config! { chain_indent: BlockIndentStyle, BlockIndentStyle::Tabbed, "Indentation of chain"; chains_overflow_last: bool, true, "Allow last call in method chain to break the line"; reorder_imports: bool, false, "Reorder import statements alphabetically"; - single_line_if_else: bool, false, "Put else on same line as closing brace for if statements"; + single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \ + expressions. A value of zero means always break \ + if-else expressions."; format_strings: bool, true, "Format string literals where necessary"; force_format_strings: bool, false, "Always format string literals"; take_source_hints: bool, true, "Retain some formatting characteristics from the source code"; diff --git a/src/expr.rs b/src/expr.rs index ebe34400014..d57c8d294e1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -36,193 +36,210 @@ use syntax::parse::classify; impl Rewrite for ast::Expr { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option { - let result = match self.node { - ast::ExprKind::Vec(ref expr_vec) => { - rewrite_array(expr_vec.iter().map(|e| &**e), - mk_sp(context.codemap.span_after(self.span, "["), self.span.hi), - context, - width, - offset) - } - ast::ExprKind::Lit(ref l) => { - match l.node { - ast::LitKind::Str(_, ast::StrStyle::Cooked) => { - rewrite_string_lit(context, l.span, width, offset) - } - _ => { - wrap_str(context.snippet(self.span), - context.config.max_width, - width, - offset) - } + format_expr(self, ExprType::SubExpression, context, width, offset) + } +} + +#[derive(PartialEq)] +enum ExprType { + Statement, + SubExpression, +} + +fn format_expr(expr: &ast::Expr, + expr_type: ExprType, + context: &RewriteContext, + width: usize, + offset: Indent) + -> Option { + let result = match expr.node { + ast::ExprKind::Vec(ref expr_vec) => { + rewrite_array(expr_vec.iter().map(|e| &**e), + mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi), + context, + width, + offset) + } + ast::ExprKind::Lit(ref l) => { + match l.node { + ast::LitKind::Str(_, ast::StrStyle::Cooked) => { + rewrite_string_lit(context, l.span, width, offset) } - } - ast::ExprKind::Call(ref callee, ref args) => { - let inner_span = mk_sp(callee.span.hi, self.span.hi); - rewrite_call(context, &**callee, args, inner_span, width, offset) - } - ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, width, offset), - ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => { - rewrite_binary_op(context, op, lhs, rhs, width, offset) - } - ast::ExprKind::Unary(ref op, ref subexpr) => { - rewrite_unary_op(context, op, subexpr, width, offset) - } - ast::ExprKind::Struct(ref path, ref fields, ref base) => { - rewrite_struct_lit(context, - path, - fields, - base.as_ref().map(|e| &**e), - self.span, - width, - offset) - } - ast::ExprKind::Tup(ref items) => { - rewrite_tuple(context, - items.iter().map(|x| &**x), - self.span, - width, - offset) - } - ast::ExprKind::While(ref cond, ref block, label) => { - Loop::new_while(None, cond, block, label).rewrite(context, width, offset) - } - ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => { - Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset) - } - ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => { - Loop::new_for(pat, cond, block, label).rewrite(context, width, offset) - } - ast::ExprKind::Loop(ref block, label) => { - Loop::new_loop(block, label).rewrite(context, width, offset) - } - ast::ExprKind::Block(ref block) => block.rewrite(context, width, offset), - ast::ExprKind::If(ref cond, ref if_block, ref else_block) => { - rewrite_if_else(context, - cond, - if_block, - else_block.as_ref().map(|e| &**e), - self.span, - None, - width, - offset, - true) - } - ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => { - rewrite_if_else(context, - cond, - if_block, - else_block.as_ref().map(|e| &**e), - self.span, - Some(pat), - width, - offset, - true) - } - ast::ExprKind::Match(ref cond, ref arms) => { - rewrite_match(context, cond, arms, width, offset, self.span) - } - ast::ExprKind::Path(ref qself, ref path) => { - rewrite_path(context, true, qself.as_ref(), path, width, offset) - } - ast::ExprKind::Assign(ref lhs, ref rhs) => { - rewrite_assignment(context, lhs, rhs, None, width, offset) - } - ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => { - rewrite_assignment(context, lhs, rhs, Some(op), width, offset) - } - ast::ExprKind::Again(ref opt_ident) => { - let id_str = match *opt_ident { - Some(ident) => format!(" {}", ident.node), - None => String::new(), - }; - wrap_str(format!("continue{}", id_str), - context.config.max_width, - width, - offset) - } - ast::ExprKind::Break(ref opt_ident) => { - let id_str = match *opt_ident { - Some(ident) => format!(" {}", ident.node), - None => String::new(), - }; - wrap_str(format!("break{}", id_str), - context.config.max_width, - width, - offset) - } - ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => { - rewrite_closure(capture, fn_decl, body, self.span, context, width, offset) - } - ast::ExprKind::Try(..) | - ast::ExprKind::Field(..) | - ast::ExprKind::TupField(..) | - ast::ExprKind::MethodCall(..) => rewrite_chain(self, context, width, offset), - ast::ExprKind::Mac(ref mac) => { - // Failure to rewrite a marco should not imply failure to - // rewrite the expression. - rewrite_macro(mac, None, context, width, offset).or_else(|| { - wrap_str(context.snippet(self.span), + _ => { + wrap_str(context.snippet(expr.span), context.config.max_width, width, offset) - }) - } - ast::ExprKind::Ret(None) => { - wrap_str("return".to_owned(), context.config.max_width, width, offset) - } - ast::ExprKind::Ret(Some(ref expr)) => { - rewrite_unary_prefix(context, "return ", &**expr, width, offset) - } - ast::ExprKind::Box(ref expr) => { - rewrite_unary_prefix(context, "box ", &**expr, width, offset) - } - ast::ExprKind::AddrOf(mutability, ref expr) => { - rewrite_expr_addrof(context, mutability, expr, width, offset) - } - ast::ExprKind::Cast(ref expr, ref ty) => { - rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset) - } - ast::ExprKind::Type(ref expr, ref ty) => { - rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset) - } - ast::ExprKind::Index(ref expr, ref index) => { - rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset) - } - ast::ExprKind::Repeat(ref expr, ref repeats) => { - rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset) - } - ast::ExprKind::Range(ref lhs, ref rhs, limits) => { - let delim = match limits { - ast::RangeLimits::HalfOpen => "..", - ast::RangeLimits::Closed => "...", - }; - - match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) { - (Some(ref lhs), Some(ref rhs)) => { - rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset) - } - (None, Some(ref rhs)) => { - rewrite_unary_prefix(context, delim, &**rhs, width, offset) - } - (Some(ref lhs), None) => { - rewrite_unary_suffix(context, delim, &**lhs, width, offset) - } - (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset), } } - // We do not format these expressions yet, but they should still - // satisfy our width restrictions. - ast::ExprKind::InPlace(..) | - ast::ExprKind::InlineAsm(..) => { - wrap_str(context.snippet(self.span), + } + ast::ExprKind::Call(ref callee, ref args) => { + let inner_span = mk_sp(callee.span.hi, expr.span.hi); + rewrite_call(context, &**callee, args, inner_span, width, offset) + } + ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, width, offset), + ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => { + rewrite_binary_op(context, op, lhs, rhs, width, offset) + } + ast::ExprKind::Unary(ref op, ref subexpr) => { + rewrite_unary_op(context, op, subexpr, width, offset) + } + ast::ExprKind::Struct(ref path, ref fields, ref base) => { + rewrite_struct_lit(context, + path, + fields, + base.as_ref().map(|e| &**e), + expr.span, + width, + offset) + } + ast::ExprKind::Tup(ref items) => { + rewrite_tuple(context, + items.iter().map(|x| &**x), + expr.span, + width, + offset) + } + ast::ExprKind::While(ref cond, ref block, label) => { + Loop::new_while(None, cond, block, label).rewrite(context, width, offset) + } + ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => { + Loop::new_while(Some(pat), cond, block, label).rewrite(context, width, offset) + } + ast::ExprKind::ForLoop(ref pat, ref cond, ref block, label) => { + Loop::new_for(pat, cond, block, label).rewrite(context, width, offset) + } + ast::ExprKind::Loop(ref block, label) => { + Loop::new_loop(block, label).rewrite(context, width, offset) + } + ast::ExprKind::Block(ref block) => block.rewrite(context, width, offset), + ast::ExprKind::If(ref cond, ref if_block, ref else_block) => { + rewrite_if_else(context, + cond, + expr_type, + if_block, + else_block.as_ref().map(|e| &**e), + expr.span, + None, + width, + offset, + true) + } + ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => { + rewrite_if_else(context, + cond, + expr_type, + if_block, + else_block.as_ref().map(|e| &**e), + expr.span, + Some(pat), + width, + offset, + true) + } + ast::ExprKind::Match(ref cond, ref arms) => { + rewrite_match(context, cond, arms, width, offset, expr.span) + } + ast::ExprKind::Path(ref qself, ref path) => { + rewrite_path(context, true, qself.as_ref(), path, width, offset) + } + ast::ExprKind::Assign(ref lhs, ref rhs) => { + rewrite_assignment(context, lhs, rhs, None, width, offset) + } + ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => { + rewrite_assignment(context, lhs, rhs, Some(op), width, offset) + } + ast::ExprKind::Again(ref opt_ident) => { + let id_str = match *opt_ident { + Some(ident) => format!(" {}", ident.node), + None => String::new(), + }; + wrap_str(format!("continue{}", id_str), + context.config.max_width, + width, + offset) + } + ast::ExprKind::Break(ref opt_ident) => { + let id_str = match *opt_ident { + Some(ident) => format!(" {}", ident.node), + None => String::new(), + }; + wrap_str(format!("break{}", id_str), + context.config.max_width, + width, + offset) + } + ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => { + rewrite_closure(capture, fn_decl, body, expr.span, context, width, offset) + } + ast::ExprKind::Try(..) | + ast::ExprKind::Field(..) | + ast::ExprKind::TupField(..) | + ast::ExprKind::MethodCall(..) => rewrite_chain(expr, context, width, offset), + ast::ExprKind::Mac(ref mac) => { + // Failure to rewrite a marco should not imply failure to + // rewrite the expression. + rewrite_macro(mac, None, context, width, offset).or_else(|| { + wrap_str(context.snippet(expr.span), context.config.max_width, width, offset) + }) + } + ast::ExprKind::Ret(None) => { + wrap_str("return".to_owned(), context.config.max_width, width, offset) + } + ast::ExprKind::Ret(Some(ref expr)) => { + rewrite_unary_prefix(context, "return ", &**expr, width, offset) + } + ast::ExprKind::Box(ref expr) => { + rewrite_unary_prefix(context, "box ", &**expr, width, offset) + } + ast::ExprKind::AddrOf(mutability, ref expr) => { + rewrite_expr_addrof(context, mutability, expr, width, offset) + } + ast::ExprKind::Cast(ref expr, ref ty) => { + rewrite_pair(&**expr, &**ty, "", " as ", "", context, width, offset) + } + ast::ExprKind::Type(ref expr, ref ty) => { + rewrite_pair(&**expr, &**ty, "", ": ", "", context, width, offset) + } + ast::ExprKind::Index(ref expr, ref index) => { + rewrite_pair(&**expr, &**index, "", "[", "]", context, width, offset) + } + ast::ExprKind::Repeat(ref expr, ref repeats) => { + rewrite_pair(&**expr, &**repeats, "[", "; ", "]", context, width, offset) + } + ast::ExprKind::Range(ref lhs, ref rhs, limits) => { + let delim = match limits { + ast::RangeLimits::HalfOpen => "..", + ast::RangeLimits::Closed => "...", + }; + + match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) { + (Some(ref lhs), Some(ref rhs)) => { + rewrite_pair(&**lhs, &**rhs, "", delim, "", context, width, offset) + } + (None, Some(ref rhs)) => { + rewrite_unary_prefix(context, delim, &**rhs, width, offset) + } + (Some(ref lhs), None) => { + rewrite_unary_suffix(context, delim, &**lhs, width, offset) + } + (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset), } - }; - result.and_then(|res| recover_comment_removed(res, self.span, context, width, offset)) - } + } + // We do not format these expressions yet, but they should still + // satisfy our width restrictions. + ast::ExprKind::InPlace(..) | + ast::ExprKind::InlineAsm(..) => { + wrap_str(context.snippet(expr.span), + context.config.max_width, + width, + offset) + } + }; + result.and_then(|res| recover_comment_removed(res, expr.span, context, width, offset)) } pub fn rewrite_pair(lhs: &LHS, @@ -470,11 +487,7 @@ fn rewrite_closure(capture: ast::CaptureBy, } fn and_one_line(x: Option) -> Option { - x.and_then(|x| if x.contains('\n') { - None - } else { - Some(x) - }) + x.and_then(|x| if x.contains('\n') { None } else { Some(x) }) } fn nop_block_collapse(block_str: Option, budget: usize) -> Option { @@ -560,15 +573,13 @@ impl Rewrite for ast::Stmt { } ast::StmtKind::Expr(ref ex, _) | ast::StmtKind::Semi(ref ex, _) => { - let suffix = if semicolon_for_stmt(self) { - ";" - } else { - "" - }; + let suffix = if semicolon_for_stmt(self) { ";" } else { "" }; - ex.rewrite(context, - context.config.max_width - offset.width() - suffix.len(), - offset) + format_expr(ex, + ExprType::Statement, + context, + context.config.max_width - offset.width() - suffix.len(), + offset) .map(|s| s + suffix) } ast::StmtKind::Mac(..) => None, @@ -704,6 +715,7 @@ fn extract_comment(span: Span, // treated as an if-let-else expression. fn rewrite_if_else(context: &RewriteContext, cond: &ast::Expr, + expr_type: ExprType, if_block: &ast::Block, else_block_opt: Option<&ast::Expr>, span: Span, @@ -726,10 +738,12 @@ fn rewrite_if_else(context: &RewriteContext, offset + 3)); // Try to format if-else on single line. - if allow_single_line && context.config.single_line_if_else { + if expr_type == ExprType::SubExpression && allow_single_line && + context.config.single_line_if_else_max_width > 0 { let trial = single_line_if_else(context, &pat_expr_string, if_block, else_block_opt, width); - if trial.is_some() { + if trial.is_some() && + trial.as_ref().unwrap().len() <= context.config.single_line_if_else_max_width { return trial; } } @@ -766,6 +780,7 @@ fn rewrite_if_else(context: &RewriteContext, ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => { rewrite_if_else(context, cond, + expr_type, if_block, next_else_block.as_ref().map(|e| &**e), mk_sp(else_block.span.lo, span.hi), @@ -777,6 +792,7 @@ fn rewrite_if_else(context: &RewriteContext, ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => { rewrite_if_else(context, cond, + expr_type, if_block, next_else_block.as_ref().map(|e| &**e), mk_sp(else_block.span.lo, span.hi), @@ -1248,11 +1264,7 @@ fn rewrite_pat_expr(context: &RewriteContext, // The expression may (partionally) fit on the current line. if width > extra_offset + 1 { - let spacer = if pat.is_some() { - " " - } else { - "" - }; + let spacer = if pat.is_some() { " " } else { "" }; let expr_rewrite = expr.rewrite(context, width - extra_offset - spacer.len(), diff --git a/src/imports.rs b/src/imports.rs index c7cedc63a98..cb644485199 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -111,11 +111,7 @@ pub fn rewrite_use_list(width: usize, } // 2 = :: - let path_separation_w = if !path_str.is_empty() { - 2 - } else { - 0 - }; + let path_separation_w = if !path_str.is_empty() { 2 } else { 0 }; // 1 = { let supp_indent = path_str.len() + path_separation_w + 1; // 1 = } @@ -140,11 +136,7 @@ pub fn rewrite_use_list(width: usize, // potentially move "self" to the front of the vector without touching // the rest of the items. let has_self = move_self_to_front(&mut items); - let first_index = if has_self { - 0 - } else { - 1 - }; + let first_index = if has_self { 0 } else { 1 }; if context.config.reorder_imports { items[1..].sort_by(|a, b| a.item.cmp(&b.item)); diff --git a/src/items.rs b/src/items.rs index b55ecd5c0ef..6cafe34a824 100644 --- a/src/items.rs +++ b/src/items.rs @@ -140,11 +140,7 @@ impl<'a> FmtVisitor<'a> { // FIXME(#21): we're dropping potential comments in between the // function keywords here. let vis = format_visibility(&item.vis); - let mut_str = if is_mutable { - "mut " - } else { - "" - }; + let mut_str = if is_mutable { "mut " } else { "" }; let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident); let offset = self.block_indent + prefix.len(); // 1 = ; @@ -265,11 +261,7 @@ impl<'a> FmtVisitor<'a> { if self.config.fn_single_line && is_simple_block_stmt(block, codemap) { let rewrite = { if let Some(ref e) = block.expr { - let suffix = if semicolon_for_expr(e) { - ";" - } else { - "" - }; + let suffix = if semicolon_for_expr(e) { ";" } else { "" }; e.rewrite(&self.get_context(), self.config.max_width - self.block_indent.width(), @@ -1280,11 +1272,7 @@ fn rewrite_fn_base(context: &RewriteContext, .rewrite(&context, context.config.max_width - indent.width(), indent)); let multi_line_ret_str = ret_str.contains('\n'); - let ret_str_len = if multi_line_ret_str { - 0 - } else { - ret_str.len() - }; + let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() }; // Args. let (mut one_line_budget, mut multi_line_budget, mut arg_indent) = diff --git a/src/lists.rs b/src/lists.rs index b35ccfe3d69..2bd4becc57f 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -208,11 +208,7 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option let first = i == 0; let last = iter.peek().is_none(); let separate = !last || trailing_separator; - let item_sep_len = if separate { - sep_len - } else { - 0 - }; + let item_sep_len = if separate { sep_len } else { 0 }; // Item string may be multi-line. Its length (used for block comment alignment) // Should be only the length of the last line. diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 2191a7812b1..7e6d1516636 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -93,13 +93,7 @@ impl<'a> FmtVisitor<'a> { fn replace_chars(string: &str) -> String { string.chars() - .map(|ch| { - if ch.is_whitespace() { - ch - } else { - 'X' - } - }) + .map(|ch| { if ch.is_whitespace() { ch } else { 'X' } }) .collect() } diff --git a/src/patterns.rs b/src/patterns.rs index 837fecd83ce..2fe16495b99 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -121,11 +121,7 @@ impl Rewrite for Pat { PatKind::Struct(ref path, ref fields, elipses) => { let path = try_opt!(rewrite_path(context, true, None, path, width, offset)); - let (elipses_str, terminator) = if elipses { - (", ..", "..") - } else { - ("", "}") - }; + let (elipses_str, terminator) = if elipses { (", ..", "..") } else { ("", "}") }; // 5 = `{` plus space before and after plus `}` plus space before. let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len())); diff --git a/src/types.rs b/src/types.rs index 8dd8c09959b..f44a10a4a37 100644 --- a/src/types.rs +++ b/src/types.rs @@ -187,11 +187,7 @@ fn rewrite_segment(expr_context: bool, let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1); let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<"); - let separator = if expr_context { - "::" - } else { - "" - }; + let separator = if expr_context { "::" } else { "" }; // 1 for < let extra_offset = 1 + separator.len(); diff --git a/src/utils.rs b/src/utils.rs index cb286723cc9..188174e6021 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -77,11 +77,7 @@ pub fn format_visibility(vis: &Visibility) -> Cow<'static, str> { Visibility::Crate(_) => Cow::from("pub(crate) "), Visibility::Restricted { ref path, .. } => { let Path { global, ref segments, .. } = **path; - let prefix = if global { - "::" - } else { - "" - }; + let prefix = if global { "::" } else { "" }; let mut segments_iter = segments.iter().map(|seg| seg.identifier.name.as_str()); Cow::from(format!("pub({}{}) ", prefix, segments_iter.join("::"))) diff --git a/src/visitor.rs b/src/visitor.rs index 8473d1685da..94ae85b4ecb 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -77,11 +77,7 @@ impl<'a> FmtVisitor<'a> { // Check if this block has braces. let snippet = self.snippet(b.span); let has_braces = snippet.starts_with("{") || snippet.starts_with("unsafe"); - let brace_compensation = if has_braces { - BytePos(1) - } else { - BytePos(0) - }; + let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) }; self.last_pos = self.last_pos + brace_compensation; self.block_indent = self.block_indent.block_indent(self.config); diff --git a/tests/config/small_tabs.toml b/tests/config/small_tabs.toml index cb4d5da5274..2a0af63eea5 100644 --- a/tests/config/small_tabs.toml +++ b/tests/config/small_tabs.toml @@ -21,7 +21,7 @@ enum_trailing_comma = true report_todo = "Always" report_fixme = "Never" reorder_imports = false -single_line_if_else = false +single_line_if_else_max_width = 0 format_strings = true chains_overflow_last = true take_source_hints = true diff --git a/tests/source/chains-block-indented-base.rs b/tests/source/chains-block-indented-base.rs index 05459dc6973..6646056690d 100644 --- a/tests/source/chains-block-indented-base.rs +++ b/tests/source/chains-block-indented-base.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chain_base_indent: Inherit // Test chain formatting with block indented base diff --git a/tests/source/chains-no-overflow.rs b/tests/source/chains-no-overflow.rs index b94ca21b258..29c092d9f1a 100644 --- a/tests/source/chains-no-overflow.rs +++ b/tests/source/chains-no-overflow.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chains_overflow_last: false // Test chain formatting without overflowing the last item. diff --git a/tests/source/chains-visual.rs b/tests/source/chains-visual.rs index a62ec4f492e..bd365ce9a4c 100644 --- a/tests/source/chains-visual.rs +++ b/tests/source/chains-visual.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chain_indent: Visual // rustfmt-chain_base_indent: Visual // Test chain formatting. diff --git a/tests/source/chains.rs b/tests/source/chains.rs index 66c982082e7..d2df8c48da6 100644 --- a/tests/source/chains.rs +++ b/tests/source/chains.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // Test chain formatting. fn main() { diff --git a/tests/source/closure.rs b/tests/source/closure.rs index 4fd5465cca0..0a0dd717586 100644 --- a/tests/source/closure.rs +++ b/tests/source/closure.rs @@ -11,21 +11,19 @@ fn main() { }; let loooooooooooooong_name = |field| { - // TODO(#27): format comments. + // format comments. if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo } else { field.span.lo }}; - let block_me = |field| if true_story() { 1 } else { 2 }; - let unblock_me = |trivial| { closure() }; let empty = |arg| {}; - let simple = |arg| { /* TODO(#27): comment formatting */ foo(arg) }; + let simple = |arg| { /* comment formatting */ foo(arg) }; let test = | | { do_something(); do_something_else(); }; diff --git a/tests/source/else-if-brace-style-always-next-line.rs b/tests/source/else-if-brace-style-always-next-line.rs index 18a8ace1f14..768aa1fe3c0 100644 --- a/tests/source/else-if-brace-style-always-next-line.rs +++ b/tests/source/else-if-brace-style-always-next-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: AlwaysNextLine fn main() { diff --git a/tests/source/else-if-brace-style-always-same-line.rs b/tests/source/else-if-brace-style-always-same-line.rs index 090b1e9a9db..7f8a3f1a3c4 100644 --- a/tests/source/else-if-brace-style-always-same-line.rs +++ b/tests/source/else-if-brace-style-always-same-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: AlwaysSameLine fn main() { diff --git a/tests/source/else-if-brace-style-closing-next-line.rs b/tests/source/else-if-brace-style-closing-next-line.rs index 755b95bd541..8784d7c08d7 100644 --- a/tests/source/else-if-brace-style-closing-next-line.rs +++ b/tests/source/else-if-brace-style-closing-next-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: ClosingNextLine fn main() { diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 40fd3309a4c..ee24e742641 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -255,3 +255,20 @@ fn ranges() { // the expr below won't compile for some reason... // let a = 0 ... ; } + +fn if_else() { + let exact = diff / + (if size == 0 { + 1 +} else { + size +}); + + let cx = tp1.x + + any * radius * + if anticlockwise { + 1.0 + } else { + -1.0 + }; +} diff --git a/tests/source/hard-tabs.rs b/tests/source/hard-tabs.rs index 290dbfc616d..c19d5d4c01f 100644 --- a/tests/source/hard-tabs.rs +++ b/tests/source/hard-tabs.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-wrap_comments: true // rustfmt-hard_tabs: true diff --git a/tests/source/single-line-if-else.rs b/tests/source/single-line-if-else.rs index 2f9c19086ff..3780ef7659f 100644 --- a/tests/source/single-line-if-else.rs +++ b/tests/source/single-line-if-else.rs @@ -1,4 +1,4 @@ -// rustfmt-single_line_if_else: true +// rustfmt-single_line_if_else_max_width: 100 // Format if-else expressions on a single line, when possible. diff --git a/tests/target/chains-block-indented-base.rs b/tests/target/chains-block-indented-base.rs index 5b9863689de..f737745b366 100644 --- a/tests/target/chains-block-indented-base.rs +++ b/tests/target/chains-block-indented-base.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chain_base_indent: Inherit // Test chain formatting with block indented base diff --git a/tests/target/chains-no-overflow.rs b/tests/target/chains-no-overflow.rs index 2c78fbd9121..a6fd0f92a8d 100644 --- a/tests/target/chains-no-overflow.rs +++ b/tests/target/chains-no-overflow.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chains_overflow_last: false // Test chain formatting without overflowing the last item. diff --git a/tests/target/chains-visual.rs b/tests/target/chains-visual.rs index d17a98e2ada..9a13d03d5a2 100644 --- a/tests/target/chains-visual.rs +++ b/tests/target/chains-visual.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-chain_indent: Visual // rustfmt-chain_base_indent: Visual // Test chain formatting. diff --git a/tests/target/chains.rs b/tests/target/chains.rs index 3004946fb56..46bf4eaf354 100644 --- a/tests/target/chains.rs +++ b/tests/target/chains.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // Test chain formatting. fn main() { diff --git a/tests/target/closure.rs b/tests/target/closure.rs index 8fe290b955a..ed8a66e2d16 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure.rs @@ -18,7 +18,7 @@ fn main() { }; let loooooooooooooong_name = |field| { - // TODO(#27): format comments. + // format comments. if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo } else { @@ -26,18 +26,12 @@ fn main() { } }; - let block_me = |field| if true_story() { - 1 - } else { - 2 - }; - let unblock_me = |trivial| closure(); let empty = |arg| {}; let simple = |arg| { - // TODO(#27): comment formatting + // comment formatting foo(arg) }; diff --git a/tests/target/else-if-brace-style-always-next-line.rs b/tests/target/else-if-brace-style-always-next-line.rs index 3fbd8b7af25..e14bf7d0182 100644 --- a/tests/target/else-if-brace-style-always-next-line.rs +++ b/tests/target/else-if-brace-style-always-next-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: AlwaysNextLine fn main() { diff --git a/tests/target/else-if-brace-style-always-same-line.rs b/tests/target/else-if-brace-style-always-same-line.rs index 393944133f7..e98d9bfc811 100644 --- a/tests/target/else-if-brace-style-always-same-line.rs +++ b/tests/target/else-if-brace-style-always-same-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: AlwaysSameLine fn main() { diff --git a/tests/target/else-if-brace-style-closing-next-line.rs b/tests/target/else-if-brace-style-closing-next-line.rs index e50f29d2921..7978e372449 100644 --- a/tests/target/else-if-brace-style-closing-next-line.rs +++ b/tests/target/else-if-brace-style-closing-next-line.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-else_if_brace_style: ClosingNextLine fn main() { diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 1378b4bf8f8..87a45a98d49 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -62,11 +62,7 @@ fn foo() -> bool { tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3 { } - let test = if true { - 5 - } else { - 3 - }; + let test = if true { 5 } else { 3 }; if cond() { something(); @@ -92,11 +88,7 @@ fn bar() { syntactically_correct(loop { sup('?'); }, - if cond { - 0 - } else { - 1 - }); + if cond { 0 } else { 1 }); let third = ..10; let infi_range = ..; @@ -277,3 +269,9 @@ fn ranges() { // the expr below won't compile for some reason... // let a = 0 ... ; } + +fn if_else() { + let exact = diff / (if size == 0 { 1 } else { size }); + + let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 }; +} diff --git a/tests/target/hard-tabs.rs b/tests/target/hard-tabs.rs index 1e00559b3f3..7fa076b888f 100644 --- a/tests/target/hard-tabs.rs +++ b/tests/target/hard-tabs.rs @@ -1,3 +1,4 @@ +// rustfmt-single_line_if_else_max_width: 0 // rustfmt-wrap_comments: true // rustfmt-hard_tabs: true diff --git a/tests/target/single-line-if-else.rs b/tests/target/single-line-if-else.rs index ec4daa727a6..ac55d062134 100644 --- a/tests/target/single-line-if-else.rs +++ b/tests/target/single-line-if-else.rs @@ -1,4 +1,4 @@ -// rustfmt-single_line_if_else: true +// rustfmt-single_line_if_else_max_width: 100 // Format if-else expressions on a single line, when possible.