Don't skip semicolon if exprs follow
This commit is contained in:
parent
3c3cf6192d
commit
8850854746
13
src/stmt.rs
13
src/stmt.rs
@ -80,13 +80,19 @@ impl<'a> Rewrite for Stmt<'a> {
|
||||
} else {
|
||||
ExprType::Statement
|
||||
};
|
||||
format_stmt(context, shape, self.as_ast_node(), expr_type)
|
||||
format_stmt(
|
||||
context,
|
||||
shape,
|
||||
self.as_ast_node(),
|
||||
expr_type,
|
||||
Some(self.is_last_expr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Rewrite for ast::Stmt {
|
||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||
format_stmt(context, shape, self, ExprType::Statement)
|
||||
format_stmt(context, shape, self, ExprType::Statement, None)
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,13 +101,14 @@ fn format_stmt(
|
||||
shape: Shape,
|
||||
stmt: &ast::Stmt,
|
||||
expr_type: ExprType,
|
||||
is_last_expr: Option<bool>,
|
||||
) -> Option<String> {
|
||||
skip_out_of_file_lines_range!(context, stmt.span());
|
||||
|
||||
let result = match stmt.kind {
|
||||
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
|
||||
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
|
||||
let suffix = if semicolon_for_stmt(context, stmt) {
|
||||
let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) {
|
||||
";"
|
||||
} else {
|
||||
""
|
||||
|
10
src/utils.rs
10
src/utils.rs
@ -292,14 +292,20 @@ pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn semicolon_for_stmt(context: &RewriteContext<'_>, stmt: &ast::Stmt) -> bool {
|
||||
pub(crate) fn semicolon_for_stmt(
|
||||
context: &RewriteContext<'_>,
|
||||
stmt: &ast::Stmt,
|
||||
is_last_expr: Option<bool>,
|
||||
) -> bool {
|
||||
match stmt.kind {
|
||||
ast::StmtKind::Semi(ref expr) => match expr.kind {
|
||||
ast::ExprKind::While(..) | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop(..) => {
|
||||
false
|
||||
}
|
||||
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
|
||||
context.config.trailing_semicolon()
|
||||
// The only time we can skip the semi-colon is if the config option is set to false
|
||||
// **and** this is the last expr (even though any following exprs are unreachable)
|
||||
context.config.trailing_semicolon() || !is_last_expr.unwrap_or(false)
|
||||
}
|
||||
_ => true,
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user