diff --git a/src/expr.rs b/src/expr.rs index 7808f891336..6eea315cf6e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1102,8 +1102,14 @@ impl<'a> Rewrite for ControlFlow<'a> { }; let block_str = { let old_val = context.is_if_else_block.replace(self.else_block.is_some()); - let result = - rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true); + let allow_single_line = + allow_single_line_if(&cond_str, self.block) && self.keyword == "if"; + + let result = if allow_single_line { + rewrite_block_inner(self.block, None, None, true, context, block_shape) + } else { + rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true) + }; context.is_if_else_block.replace(old_val); result? }; @@ -1158,6 +1164,30 @@ impl<'a> Rewrite for ControlFlow<'a> { } } +fn allow_single_line_if(result: &str, block: &ast::Block) -> bool { + if result.contains('\n') { + return false; + } + + if block.stmts.len() == 0 { + return true; + } + if block.stmts.len() == 1 { + return is_simple_stmt(&block.stmts[0]); + } + false +} + +fn is_simple_stmt(stmt: &ast::Stmt) -> bool { + match stmt.kind { + ast::StmtKind::Expr(ref expr) => match expr.kind { + ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true, + _ => false, + }, + _ => false, + } +} + fn rewrite_label(opt_label: Option) -> Cow<'static, str> { match opt_label { Some(label) => Cow::from(format!("{}: ", label.ident)), diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 879c551ea49..056a94b2676 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -79,7 +79,7 @@ fn bar() { let bar = 5 ; let nonsense = (10 .. 0)..(0..10); - loop{if true {break}} + loop{if true {break;}} let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, diff --git a/tests/source/single-line-if-else.rs b/tests/source/single-line-if-else.rs index bcde390d116..832a53dedb8 100644 --- a/tests/source/single-line-if-else.rs +++ b/tests/source/single-line-if-else.rs @@ -35,6 +35,22 @@ fn main() { do_something() } + let a = if x { 1 } else { 3 }; + + // if may be formatted on a single line if it is "short" + // and only contain a single expression + if true { return } + + if true { + return + } + + if true { return; } + + if a { let y = 1; return y } + + for i in 0..2 { if g == true { continue } } + let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb }; let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else { diff --git a/tests/target/single-line-if-else.rs b/tests/target/single-line-if-else.rs index 98fd793cba2..002f576b01a 100644 --- a/tests/target/single-line-if-else.rs +++ b/tests/target/single-line-if-else.rs @@ -42,6 +42,27 @@ fn main() { do_something() } + let a = if x { 1 } else { 3 }; + + // if may be formatted on a single line if it is "short" + // and only contain a single expression + if true { return } + + if true { return } + + if true { + return; + } + + if a { + let y = 1; + return y; + } + + for i in 0..2 { + if g == true { continue } + } + let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else {