Skip to content

Force semicolons after break/continue/return. Remove after blocks. #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,

if prelim_tactic == ListTactic::HorizontalVertical && fields.len() > 1 {
prelim_tactic = ListTactic::LimitedHorizontalVertical(context.config.struct_lit_width);
};
}

definitive_tactic(&item_vec, prelim_tactic, h_budget)
};
Expand Down
45 changes: 38 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
self.format_missing_with_indent(stmt.span.lo);
let suffix = if let ast::Stmt_::StmtExpr(..) = stmt.node {
""
} else {
let suffix = if semicolon_for_stmt(stmt) {
";"
} else {
""
};

// 1 = trailing semicolon;
let rewrite = ex.rewrite(&self.get_context(),
self.config.max_width - self.block_indent.width() -
suffix.len(),
Expand Down Expand Up @@ -110,6 +108,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
Some(ref e) => {
self.format_missing_with_indent(e.span.lo);
self.visit_expr(e);

if semicolon_for_expr(e) {
self.buffer.push_str(";");
}
}
None => {}
}
Expand Down Expand Up @@ -215,7 +217,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}
ast::Item_::ItemMac(..) => {
self.format_missing_with_indent(item.span.lo);
// TODO: we cannot format these yet, because of a bad span.
// FIXME: we cannot format these yet, because of a bad span.
// See rust lang issue #28424.
// visit::walk_item(self, item);
}
Expand Down Expand Up @@ -245,7 +247,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.last_pos = ti.span.hi;
}
}
// TODO: format trait types.
// TODO(#78): format trait types.

visit::walk_trait_item(self, ti)
}
Expand Down Expand Up @@ -399,6 +401,35 @@ impl<'a> FmtVisitor<'a> {
}
}

fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
match stmt.node {
ast::Stmt_::StmtSemi(ref expr, _) => {
match expr.node {
ast::Expr_::ExprWhile(..) |
ast::Expr_::ExprWhileLet(..) |
ast::Expr_::ExprIf(..) |
ast::Expr_::ExprIfLet(..) |
ast::Expr_::ExprBlock(..) |
ast::Expr_::ExprLoop(..) |
ast::Expr_::ExprForLoop(..) |
ast::Expr_::ExprMatch(..) => false,
_ => true,
}
}
ast::Stmt_::StmtExpr(..) => false,
_ => true,
}
}

fn semicolon_for_expr(expr: &ast::Expr) -> bool {
match expr.node {
ast::Expr_::ExprRet(..) |
ast::Expr_::ExprAgain(..) |
ast::Expr_::ExprBreak(..) => true,
_ => false,
}
}

impl<'a> Rewrite for [ast::Attribute] {
fn rewrite(&self, context: &RewriteContext, _: usize, offset: Indent) -> Option<String> {
let mut result = String::new();
Expand Down
8 changes: 7 additions & 1 deletion tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn bar() {
let bar = 5 ;
let nonsense = (10 .. 0)..(0..10);

loop{if true {break;}}
loop{if true {break}}

let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
Expand Down Expand Up @@ -226,3 +226,9 @@ fn repeats() {
let x = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc; x + y + z ];
let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc; xxxxx + yyyyy + zzzzz ];
}

fn blocks() {
if 1 + 1 == 2 {
println!("yay arithmetix!");
};
}
8 changes: 7 additions & 1 deletion tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn foo() -> bool {
result
} else {
4
};
}

if let Some(x) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
// Nothing
Expand Down Expand Up @@ -248,3 +248,9 @@ fn repeats() {
let y = [aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
cccccccccccccccc; xxxxx + yyyyy + zzzzz];
}

fn blocks() {
if 1 + 1 == 2 {
println!("yay arithmetix!");
}
}
2 changes: 1 addition & 1 deletion tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ fn issue383() {
match resolution.last_private {
LastImport{..} => false,
_ => true,
};
}
}