Merge pull request #1783 from topecongiro/trailing-semicolon
Add trailing_semicolon config option
This commit is contained in:
commit
43af9c84b1
@ -1698,6 +1698,27 @@ let Lorem {
|
|||||||
|
|
||||||
See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
|
See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
|
||||||
|
|
||||||
|
## `trailing_semicolon`
|
||||||
|
|
||||||
|
Add trailing semicolon after break, continue and return
|
||||||
|
|
||||||
|
- **Default value**: `true`
|
||||||
|
- **Possible values**: `true`, `false`
|
||||||
|
|
||||||
|
#### `true`:
|
||||||
|
```rust
|
||||||
|
fn foo() -> usize {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `false`:
|
||||||
|
```rust
|
||||||
|
fn foo() -> usize {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## `type_punctuation_density`
|
## `type_punctuation_density`
|
||||||
|
|
||||||
Determines if `+` or `=` are wrapped in spaces in the punctuation of types
|
Determines if `+` or `=` are wrapped in spaces in the punctuation of types
|
||||||
|
@ -519,6 +519,7 @@ pub fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
|
|||||||
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
|
impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
|
||||||
trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
|
trailing_comma: SeparatorTactic, SeparatorTactic::Vertical,
|
||||||
"How to handle trailing commas for lists";
|
"How to handle trailing commas for lists";
|
||||||
|
trailing_semicolon: bool, true, "Add trailing semicolon after break, continue and return";
|
||||||
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
|
fn_empty_single_line: bool, true, "Put empty-body functions on a single line";
|
||||||
fn_single_line: bool, false, "Put single-expression functions on a single line";
|
fn_single_line: bool, false, "Put single-expression functions on a single line";
|
||||||
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
|
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
|
||||||
|
@ -906,7 +906,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|||||||
let result = match self.node {
|
let result = match self.node {
|
||||||
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
|
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
|
||||||
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
|
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
|
||||||
let suffix = if semicolon_for_stmt(self) { ";" } else { "" };
|
let suffix = if semicolon_for_stmt(context, self) {
|
||||||
|
";"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
};
|
||||||
|
|
||||||
format_expr(
|
format_expr(
|
||||||
ex,
|
ex,
|
||||||
|
@ -352,7 +352,11 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
|
|||||||
if let Some(ref stmt) = block.stmts.first() {
|
if let Some(ref stmt) = block.stmts.first() {
|
||||||
match stmt_expr(stmt) {
|
match stmt_expr(stmt) {
|
||||||
Some(e) => {
|
Some(e) => {
|
||||||
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
|
let suffix = if semicolon_for_expr(&self.get_context(), e) {
|
||||||
|
";"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
};
|
||||||
|
|
||||||
format_expr(
|
format_expr(
|
||||||
&e,
|
&e,
|
||||||
|
11
src/utils.rs
11
src/utils.rs
@ -156,21 +156,26 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn semicolon_for_expr(expr: &ast::Expr) -> bool {
|
pub fn semicolon_for_expr(context: &RewriteContext, expr: &ast::Expr) -> bool {
|
||||||
match expr.node {
|
match expr.node {
|
||||||
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true,
|
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => {
|
||||||
|
context.config.trailing_semicolon()
|
||||||
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn semicolon_for_stmt(stmt: &ast::Stmt) -> bool {
|
pub fn semicolon_for_stmt(context: &RewriteContext, stmt: &ast::Stmt) -> bool {
|
||||||
match stmt.node {
|
match stmt.node {
|
||||||
ast::StmtKind::Semi(ref expr) => match expr.node {
|
ast::StmtKind::Semi(ref expr) => match expr.node {
|
||||||
ast::ExprKind::While(..) |
|
ast::ExprKind::While(..) |
|
||||||
ast::ExprKind::WhileLet(..) |
|
ast::ExprKind::WhileLet(..) |
|
||||||
ast::ExprKind::Loop(..) |
|
ast::ExprKind::Loop(..) |
|
||||||
ast::ExprKind::ForLoop(..) => false,
|
ast::ExprKind::ForLoop(..) => false,
|
||||||
|
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
|
||||||
|
context.config.trailing_semicolon()
|
||||||
|
}
|
||||||
_ => true,
|
_ => true,
|
||||||
},
|
},
|
||||||
ast::StmtKind::Expr(..) => false,
|
ast::StmtKind::Expr(..) => false,
|
||||||
|
@ -144,7 +144,7 @@ pub fn visit_block(&mut self, b: &ast::Block) {
|
|||||||
|
|
||||||
if !b.stmts.is_empty() {
|
if !b.stmts.is_empty() {
|
||||||
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
|
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
|
||||||
if utils::semicolon_for_expr(expr) {
|
if utils::semicolon_for_expr(&self.get_context(), expr) {
|
||||||
self.buffer.push_str(";");
|
self.buffer.push_str(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
tests/target/configs-trailing_semicolon-false.rs
Normal file
27
tests/target/configs-trailing_semicolon-false.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// rustfmt-trailing_semicolon: false
|
||||||
|
|
||||||
|
#![feature(loop_break_value)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
'a: loop {
|
||||||
|
break 'a
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut done = false;
|
||||||
|
'b: while !done {
|
||||||
|
done = true;
|
||||||
|
continue 'b
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = loop {
|
||||||
|
break 5
|
||||||
|
};
|
||||||
|
|
||||||
|
let x = 'c: loop {
|
||||||
|
break 'c 5
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() -> usize {
|
||||||
|
return 0
|
||||||
|
}
|
27
tests/target/configs-trailing_semicolon-true.rs
Normal file
27
tests/target/configs-trailing_semicolon-true.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// rustfmt-trailing_semicolon: true
|
||||||
|
|
||||||
|
#![feature(loop_break_value)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
'a: loop {
|
||||||
|
break 'a;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut done = false;
|
||||||
|
'b: while !done {
|
||||||
|
done = true;
|
||||||
|
continue 'b;
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = loop {
|
||||||
|
break 5;
|
||||||
|
};
|
||||||
|
|
||||||
|
let x = 'c: loop {
|
||||||
|
break 'c 5;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() -> usize {
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user