Skip to content

Commit dfa8ae4

Browse files
committed
Merge pull request #464 from marcusklaas/semicolonz
Force semicolons after break/continue/return. Remove after blocks.
2 parents 5ac3be2 + b039e3a commit dfa8ae4

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
13221322

13231323
if prelim_tactic == ListTactic::HorizontalVertical && fields.len() > 1 {
13241324
prelim_tactic = ListTactic::LimitedHorizontalVertical(context.config.struct_lit_width);
1325-
};
1325+
}
13261326

13271327
definitive_tactic(&item_vec, prelim_tactic, h_budget)
13281328
};

src/visitor.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
5959
}
6060
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
6161
self.format_missing_with_indent(stmt.span.lo);
62-
let suffix = if let ast::Stmt_::StmtExpr(..) = stmt.node {
63-
""
64-
} else {
62+
let suffix = if semicolon_for_stmt(stmt) {
6563
";"
64+
} else {
65+
""
6666
};
67-
68-
// 1 = trailing semicolon;
6967
let rewrite = ex.rewrite(&self.get_context(),
7068
self.config.max_width - self.block_indent.width() -
7169
suffix.len(),
@@ -110,6 +108,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
110108
Some(ref e) => {
111109
self.format_missing_with_indent(e.span.lo);
112110
self.visit_expr(e);
111+
112+
if semicolon_for_expr(e) {
113+
self.buffer.push_str(";");
114+
}
113115
}
114116
None => {}
115117
}
@@ -215,7 +217,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
215217
}
216218
ast::Item_::ItemMac(..) => {
217219
self.format_missing_with_indent(item.span.lo);
218-
// TODO: we cannot format these yet, because of a bad span.
220+
// FIXME: we cannot format these yet, because of a bad span.
219221
// See rust lang issue #28424.
220222
// visit::walk_item(self, item);
221223
}
@@ -245,7 +247,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
245247
self.last_pos = ti.span.hi;
246248
}
247249
}
248-
// TODO: format trait types.
250+
// TODO(#78): format trait types.
249251

250252
visit::walk_trait_item(self, ti)
251253
}
@@ -399,6 +401,35 @@ impl<'a> FmtVisitor<'a> {
399401
}
400402
}
401403

404+
fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
405+
match stmt.node {
406+
ast::Stmt_::StmtSemi(ref expr, _) => {
407+
match expr.node {
408+
ast::Expr_::ExprWhile(..) |
409+
ast::Expr_::ExprWhileLet(..) |
410+
ast::Expr_::ExprIf(..) |
411+
ast::Expr_::ExprIfLet(..) |
412+
ast::Expr_::ExprBlock(..) |
413+
ast::Expr_::ExprLoop(..) |
414+
ast::Expr_::ExprForLoop(..) |
415+
ast::Expr_::ExprMatch(..) => false,
416+
_ => true,
417+
}
418+
}
419+
ast::Stmt_::StmtExpr(..) => false,
420+
_ => true,
421+
}
422+
}
423+
424+
fn semicolon_for_expr(expr: &ast::Expr) -> bool {
425+
match expr.node {
426+
ast::Expr_::ExprRet(..) |
427+
ast::Expr_::ExprAgain(..) |
428+
ast::Expr_::ExprBreak(..) => true,
429+
_ => false,
430+
}
431+
}
432+
402433
impl<'a> Rewrite for [ast::Attribute] {
403434
fn rewrite(&self, context: &RewriteContext, _: usize, offset: Indent) -> Option<String> {
404435
let mut result = String::new();

tests/source/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn bar() {
7070
let bar = 5 ;
7171
let nonsense = (10 .. 0)..(0..10);
7272

73-
loop{if true {break;}}
73+
loop{if true {break}}
7474

7575
let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
7676
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
@@ -226,3 +226,9 @@ fn repeats() {
226226
let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
227227
let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
228228
}
229+
230+
fn blocks() {
231+
if 1 + 1 == 2 {
232+
println!("yay arithmetix!");
233+
};
234+
}

tests/target/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn foo() -> bool {
4040
result
4141
} else {
4242
4
43-
};
43+
}
4444

4545
if let Some(x) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
4646
// Nothing
@@ -248,3 +248,9 @@ fn repeats() {
248248
let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
249249
cccccccccccccccc; xxxxx + yyyyy + zzzzz];
250250
}
251+
252+
fn blocks() {
253+
if 1 + 1 == 2 {
254+
println!("yay arithmetix!");
255+
}
256+
}

tests/target/match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,5 @@ fn issue383() {
215215
match resolution.last_private {
216216
LastImport{..} => false,
217217
_ => true,
218-
};
218+
}
219219
}

0 commit comments

Comments
 (0)