Don't skip semicolon if exprs follow

This commit is contained in:
Cosmic Horror 2023-06-23 19:07:12 -06:00 committed by Caleb Cartwright
parent 3c3cf6192d
commit 8850854746
2 changed files with 18 additions and 5 deletions

View File

@ -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 {
""

View File

@ -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,
},