Skip to content

Commit 2fb66cd

Browse files
committed
Add trailing_semicolon config option
trailing_semicolon controls whether to add a trailing semicolon after break, continue and return.
1 parent 2725636 commit 2fb66cd

File tree

8 files changed

+95
-6
lines changed

8 files changed

+95
-6
lines changed

Configurations.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,27 @@ let Lorem {
16981698

16991699
See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
17001700

1701+
## `trailing_semicolon`
1702+
1703+
Add trailing semicolon after break, continue and return
1704+
1705+
- **Default value**: `true`
1706+
- **Possible values**: `true`, `false`
1707+
1708+
#### `true`:
1709+
```rust
1710+
fn foo() -> usize {
1711+
return 0;
1712+
}
1713+
```
1714+
1715+
#### `false`:
1716+
```rust
1717+
fn foo() -> usize {
1718+
return 0
1719+
}
1720+
```
1721+
17011722
## `type_punctuation_density`
17021723

17031724
Determines if `+` or `=` are wrapped in spaces in the punctuation of types

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ create_config! {
519519
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
520520
trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
521521
"How to handle trailing commas for lists";
522+
trailing_semicolon: bool, true, "Add trailing semicolon after break, continue and return";
522523
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
523524
fn_single_line: bool, false, "Put single-expression functions on a single line";
524525
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,

src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,11 @@ impl Rewrite for ast::Stmt {
906906
let result = match self.node {
907907
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
908908
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
909-
let suffix = if semicolon_for_stmt(self) { ";" } else { "" };
909+
let suffix = if semicolon_for_stmt(context, self) {
910+
";"
911+
} else {
912+
""
913+
};
910914

911915
format_expr(
912916
ex,

src/items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ impl<'a> FmtVisitor<'a> {
352352
if let Some(ref stmt) = block.stmts.first() {
353353
match stmt_expr(stmt) {
354354
Some(e) => {
355-
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
355+
let suffix = if semicolon_for_expr(&self.get_context(), e) {
356+
";"
357+
} else {
358+
""
359+
};
356360

357361
format_expr(
358362
&e,

src/utils.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,26 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
156156
}
157157

158158
#[inline]
159-
pub fn semicolon_for_expr(expr: &ast::Expr) -> bool {
159+
pub fn semicolon_for_expr(context: &RewriteContext, expr: &ast::Expr) -> bool {
160160
match expr.node {
161-
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true,
161+
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
162+
context.config.trailing_semicolon()
163+
}
162164
_ => false,
163165
}
164166
}
165167

166168
#[inline]
167-
pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
169+
pub fn semicolon_for_stmt(context: &RewriteContext, stmt: &ast::Stmt) -> bool {
168170
match stmt.node {
169171
ast::StmtKind::Semi(ref expr) => match expr.node {
170172
ast::ExprKind::While(..) |
171173
ast::ExprKind::WhileLet(..) |
172174
ast::ExprKind::Loop(..) |
173175
ast::ExprKind::ForLoop(..) => false,
176+
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
177+
context.config.trailing_semicolon()
178+
}
174179
_ => true,
175180
},
176181
ast::StmtKind::Expr(..) => false,

src/visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a> FmtVisitor<'a> {
144144

145145
if !b.stmts.is_empty() {
146146
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
147-
if utils::semicolon_for_expr(expr) {
147+
if utils::semicolon_for_expr(&self.get_context(), expr) {
148148
self.buffer.push_str(";");
149149
}
150150
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// rustfmt-trailing_semicolon: false
2+
3+
#![feature(loop_break_value)]
4+
5+
fn main() {
6+
'a: loop {
7+
break 'a
8+
}
9+
10+
let mut done = false;
11+
'b: while !done {
12+
done = true;
13+
continue 'b
14+
}
15+
16+
let x = loop {
17+
break 5
18+
};
19+
20+
let x = 'c: loop {
21+
break 'c 5
22+
};
23+
}
24+
25+
fn foo() -> usize {
26+
return 0
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// rustfmt-trailing_semicolon: true
2+
3+
#![feature(loop_break_value)]
4+
5+
fn main() {
6+
'a: loop {
7+
break 'a;
8+
}
9+
10+
let mut done = false;
11+
'b: while !done {
12+
done = true;
13+
continue 'b;
14+
}
15+
16+
let x = loop {
17+
break 5;
18+
};
19+
20+
let x = 'c: loop {
21+
break 'c 5;
22+
};
23+
}
24+
25+
fn foo() -> usize {
26+
return 0;
27+
}

0 commit comments

Comments
 (0)