From 885085474653573cb3c49ed29cd3030555bfcae8 Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Fri, 23 Jun 2023 19:07:12 -0600 Subject: [PATCH] Don't skip semicolon if exprs follow --- src/stmt.rs | 13 ++++++++++--- src/utils.rs | 10 ++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/stmt.rs b/src/stmt.rs index 0b3854425ea5..0148de8190b8 100644 --- a/src/stmt.rs +++ b/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 { - 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, ) -> Option { 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 { "" diff --git a/src/utils.rs b/src/utils.rs index 890a05b8c825..e768ae0a1bc2 100644 --- a/src/utils.rs +++ b/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 { 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, },