diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 001ffb649c6..e6a86e04b9e 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -94,11 +94,13 @@ fn execute() -> i32 { print_usage_to_stderr(&opts, &e.to_string()); failure } - Ok(status) => if status.success() { - success - } else { - status.code().unwrap_or(failure) - }, + Ok(status) => { + if status.success() { + success + } else { + status.code().unwrap_or(failure) + } + } } } diff --git a/src/config.rs b/src/config.rs index ce045502c9c..2b4665729c4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -568,9 +568,11 @@ pub fn get_toml_path(dir: &Path) -> Result, Error> { Ok(ref md) if md.is_file() => return Ok(Some(config_file)), // Return the error if it's something other than `NotFound`; otherwise we didn't // find the project file yet, and continue searching. - Err(e) => if e.kind() != ErrorKind::NotFound { - return Err(e); - }, + Err(e) => { + if e.kind() != ErrorKind::NotFound { + return Err(e); + } + } _ => {} } } diff --git a/src/expr.rs b/src/expr.rs index 1faa109c50b..fc5d1c92025 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -462,18 +462,20 @@ where None => DefinitiveListTactic::Vertical, } } - IndentStyle::Visual => if has_long_item || items.iter().any(ListItem::is_multiline) { - definitive_tactic( - &items, - ListTactic::LimitedHorizontalVertical( - context.config.width_heuristics().array_width, - ), - Separator::Comma, - nested_shape.width, - ) - } else { - DefinitiveListTactic::Mixed - }, + IndentStyle::Visual => { + if has_long_item || items.iter().any(ListItem::is_multiline) { + definitive_tactic( + &items, + ListTactic::LimitedHorizontalVertical( + context.config.width_heuristics().array_width, + ), + Separator::Comma, + nested_shape.width, + ) + } else { + DefinitiveListTactic::Mixed + } + } }; let ends_with_newline = tactic.ends_with_newline(context.config.indent_style()); @@ -1500,8 +1502,8 @@ fn flatten_arm_body<'a>(context: &'a RewriteContext, body: &'a ast::Expr) -> (bo { if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node { ( - !context.config.force_multiline_blocks() && expr.can_be_overflowed(context, 1), - &**expr, + !context.config.force_multiline_blocks() && can_extend_match_arm_body(expr), + &*expr, ) } else { (false, &*body) @@ -1723,6 +1725,33 @@ fn rewrite_pat_expr( .map(|expr_rw| format!("\n{}{}", nested_indent_str, expr_rw)) } +fn can_extend_match_arm_body(body: &ast::Expr) -> bool { + match body.node { + // We do not allow `if` to stay on the same line, since we could easily mistake + // `pat => if cond { ... }` and `pat if cond => { ... }`. + ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => false, + ast::ExprKind::ForLoop(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) + | ast::ExprKind::Match(..) + | ast::ExprKind::Block(..) + | ast::ExprKind::Closure(..) + | ast::ExprKind::Array(..) + | ast::ExprKind::Call(..) + | ast::ExprKind::MethodCall(..) + | ast::ExprKind::Mac(..) + | ast::ExprKind::Struct(..) + | ast::ExprKind::Tup(..) => true, + ast::ExprKind::AddrOf(_, ref expr) + | ast::ExprKind::Box(ref expr) + | ast::ExprKind::Try(ref expr) + | ast::ExprKind::Unary(_, ref expr) + | ast::ExprKind::Cast(ref expr, _) => can_extend_match_arm_body(expr), + _ => false, + } +} + pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option { match l.node { ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape), diff --git a/src/issues.rs b/src/issues.rs index 58c9dd5d7ab..8bb1218f88a 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -190,19 +190,25 @@ impl BadIssueSeeker { } match part { - NumberPart::OpenParen => if c != '(' { - return IssueClassification::Bad(issue); - } else { - part = NumberPart::Pound; - }, - NumberPart::Pound => if c == '#' { - part = NumberPart::Number; - }, - NumberPart::Number => if c >= '0' && c <= '9' { - part = NumberPart::CloseParen; - } else { - return IssueClassification::Bad(issue); - }, + NumberPart::OpenParen => { + if c != '(' { + return IssueClassification::Bad(issue); + } else { + part = NumberPart::Pound; + } + } + NumberPart::Pound => { + if c == '#' { + part = NumberPart::Number; + } + } + NumberPart::Number => { + if c >= '0' && c <= '9' { + part = NumberPart::CloseParen; + } else { + return IssueClassification::Bad(issue); + } + } NumberPart::CloseParen => {} } diff --git a/src/items.rs b/src/items.rs index 65dce5ac9b4..320c7bb1c0d 100644 --- a/src/items.rs +++ b/src/items.rs @@ -550,12 +550,14 @@ impl<'a> FmtVisitor<'a> { self.block_indent, Some(one_line_width), )?, - ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr { - let lhs = format!("{} =", field.node.name); - rewrite_assign_rhs(&context, lhs, &**expr, shape)? - } else { - field.node.name.to_string() - }, + ast::VariantData::Unit(..) => { + if let Some(ref expr) = field.node.disr_expr { + let lhs = format!("{} =", field.node.name); + rewrite_assign_rhs(&context, lhs, &**expr, shape)? + } else { + field.node.name.to_string() + } + } }; let attrs_extendable = attrs_str.is_empty() @@ -643,11 +645,13 @@ pub fn format_impl( _ if last_line_contains_single_line_comment(&result) => result.push_str(&sep), BraceStyle::AlwaysNextLine => result.push_str(&sep), BraceStyle::PreferSameLine => result.push(' '), - BraceStyle::SameLineWhere => if !where_clause_str.is_empty() { - result.push_str(&sep); - } else { - result.push(' '); - }, + BraceStyle::SameLineWhere => { + if !where_clause_str.is_empty() { + result.push_str(&sep); + } else { + result.push(' '); + } + } } result.push('{'); @@ -1039,14 +1043,16 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) result.push_str(&offset.to_string(context.config)); } BraceStyle::PreferSameLine => result.push(' '), - BraceStyle::SameLineWhere => if !where_clause_str.is_empty() - && (!trait_items.is_empty() || result.contains('\n')) - { - result.push('\n'); - result.push_str(&offset.to_string(context.config)); - } else { - result.push(' '); - }, + BraceStyle::SameLineWhere => { + if !where_clause_str.is_empty() + && (!trait_items.is_empty() || result.contains('\n')) + { + result.push('\n'); + result.push_str(&offset.to_string(context.config)); + } else { + result.push(' '); + } + } } result.push('{'); diff --git a/src/macros.rs b/src/macros.rs index 8c8b50b7ca3..9247a75ce86 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -126,11 +126,13 @@ pub fn rewrite_macro( let macro_name = match extra_ident { None => format!("{}!", mac.node.path), - Some(ident) => if ident == symbol::keywords::Invalid.ident() { - format!("{}!", mac.node.path) - } else { - format!("{}! {}", mac.node.path, ident) - }, + Some(ident) => { + if ident == symbol::keywords::Invalid.ident() { + format!("{}!", mac.node.path) + } else { + format!("{}! {}", mac.node.path, ident) + } + } }; let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) { diff --git a/src/patterns.rs b/src/patterns.rs index f4bb59e5127..90a47d785f0 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -51,11 +51,13 @@ impl Rewrite for Pat { Some(format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat)) } - PatKind::Wild => if 1 <= shape.width { - Some("_".to_owned()) - } else { - None - }, + PatKind::Wild => { + if 1 <= shape.width { + Some("_".to_owned()) + } else { + None + } + } PatKind::Range(ref lhs, ref rhs, ref end_kind) => { let infix = match *end_kind { RangeEnd::Included(RangeSyntax::DotDotDot) => "...", diff --git a/src/types.rs b/src/types.rs index cad5195428f..92a1adbbc8e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -720,11 +720,13 @@ impl Rewrite for ast::Ty { SeparatorPlace::Back, ) } - ast::TyKind::Infer => if shape.width >= 1 { - Some("_".to_owned()) - } else { - None - }, + ast::TyKind::Infer => { + if shape.width >= 1 { + Some("_".to_owned()) + } else { + None + } + } ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape), ast::TyKind::Never => Some(String::from("!")), ast::TyKind::Mac(..) => None, diff --git a/src/visitor.rs b/src/visitor.rs index 85a8fb38f07..2896b39b09d 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -321,10 +321,12 @@ impl<'a> FmtVisitor<'a> { attrs = &filtered_attrs; } } - _ => if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) { - self.push_rewrite(item.span, None); - return; - }, + _ => { + if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) { + self.push_rewrite(item.span, None); + return; + } + } } match item.node { diff --git a/tests/target/configs-force_multiline_block-false.rs b/tests/target/configs-force_multiline_block-false.rs index 11ccc9a7512..7cb4cac1d69 100644 --- a/tests/target/configs-force_multiline_block-false.rs +++ b/tests/target/configs-force_multiline_block-false.rs @@ -3,9 +3,11 @@ fn main() { match lorem { - Lorem::Ipsum => if ipsum { - println!("dolor"); - }, + Lorem::Ipsum => { + if ipsum { + println!("dolor"); + } + } Lorem::Dolor => println!("amet"), } }