Skip to content

Commit 58fd26a

Browse files
committed
first draft, tests OK
1 parent 6356fca commit 58fd26a

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/expr.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,14 @@ impl<'a> Rewrite for ControlFlow<'a> {
11021102
};
11031103
let block_str = {
11041104
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
1105-
let result =
1106-
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true);
1105+
let allow_single_line =
1106+
allow_single_line_if(&cond_str, self.block) && self.keyword == "if";
1107+
1108+
let result = if allow_single_line {
1109+
rewrite_block_inner(self.block, None, None, true, context, block_shape)
1110+
} else {
1111+
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true)
1112+
};
11071113
context.is_if_else_block.replace(old_val);
11081114
result?
11091115
};
@@ -1158,6 +1164,30 @@ impl<'a> Rewrite for ControlFlow<'a> {
11581164
}
11591165
}
11601166

1167+
fn allow_single_line_if(result: &str, block: &ast::Block) -> bool {
1168+
if result.contains('\n') {
1169+
return false;
1170+
}
1171+
1172+
if block.stmts.len() == 0 {
1173+
return true;
1174+
}
1175+
if block.stmts.len() == 1 {
1176+
return is_simple_stmt(&block.stmts[0]);
1177+
}
1178+
false
1179+
}
1180+
1181+
fn is_simple_stmt(stmt: &ast::Stmt) -> bool {
1182+
match stmt.kind {
1183+
ast::StmtKind::Expr(ref expr) => match expr.kind {
1184+
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true,
1185+
_ => false,
1186+
},
1187+
_ => false,
1188+
}
1189+
}
1190+
11611191
fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
11621192
match opt_label {
11631193
Some(label) => Cow::from(format!("{}: ", label.ident)),

tests/source/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn bar() {
7979
let bar = 5 ;
8080
let nonsense = (10 .. 0)..(0..10);
8181

82-
loop{if true {break}}
82+
loop{if true {break;}}
8383

8484
let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
8585
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,

tests/source/single-line-if-else.rs

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ fn main() {
3535
do_something()
3636
}
3737

38+
let a = if x { 1 } else { 3 };
39+
40+
// if may be formatted on a single line if it is "short"
41+
// and only contain a single expression
42+
if true { return }
43+
44+
if true {
45+
return
46+
}
47+
48+
if true { return; }
49+
50+
if a { let y = 1; return y }
51+
52+
for i in 0..2 { if g == true { continue } }
53+
3854
let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb };
3955

4056
let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else {

tests/target/single-line-if-else.rs

+21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ fn main() {
4242
do_something()
4343
}
4444

45+
let a = if x { 1 } else { 3 };
46+
47+
// if may be formatted on a single line if it is "short"
48+
// and only contain a single expression
49+
if true { return }
50+
51+
if true { return }
52+
53+
if true {
54+
return;
55+
}
56+
57+
if a {
58+
let y = 1;
59+
return y;
60+
}
61+
62+
for i in 0..2 {
63+
if g == true { continue }
64+
}
65+
4566
let x = if veeeeeeeeery_loooooong_condition() {
4667
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4768
} else {

0 commit comments

Comments
 (0)