Skip to content

Commit 8850854

Browse files
CosmicHorrorDevcalebcartwright
authored andcommitted
Don't skip semicolon if exprs follow
1 parent 3c3cf61 commit 8850854

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/stmt.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,19 @@ impl<'a> Rewrite for Stmt<'a> {
8080
} else {
8181
ExprType::Statement
8282
};
83-
format_stmt(context, shape, self.as_ast_node(), expr_type)
83+
format_stmt(
84+
context,
85+
shape,
86+
self.as_ast_node(),
87+
expr_type,
88+
Some(self.is_last_expr()),
89+
)
8490
}
8591
}
8692

8793
impl Rewrite for ast::Stmt {
8894
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
89-
format_stmt(context, shape, self, ExprType::Statement)
95+
format_stmt(context, shape, self, ExprType::Statement, None)
9096
}
9197
}
9298

@@ -95,13 +101,14 @@ fn format_stmt(
95101
shape: Shape,
96102
stmt: &ast::Stmt,
97103
expr_type: ExprType,
104+
is_last_expr: Option<bool>,
98105
) -> Option<String> {
99106
skip_out_of_file_lines_range!(context, stmt.span());
100107

101108
let result = match stmt.kind {
102109
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
103110
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
104-
let suffix = if semicolon_for_stmt(context, stmt) {
111+
let suffix = if semicolon_for_stmt(context, stmt, is_last_expr) {
105112
";"
106113
} else {
107114
""

src/utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,20 @@ pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr)
292292
}
293293

294294
#[inline]
295-
pub(crate) fn semicolon_for_stmt(context: &RewriteContext<'_>, stmt: &ast::Stmt) -> bool {
295+
pub(crate) fn semicolon_for_stmt(
296+
context: &RewriteContext<'_>,
297+
stmt: &ast::Stmt,
298+
is_last_expr: Option<bool>,
299+
) -> bool {
296300
match stmt.kind {
297301
ast::StmtKind::Semi(ref expr) => match expr.kind {
298302
ast::ExprKind::While(..) | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop(..) => {
299303
false
300304
}
301305
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
302-
context.config.trailing_semicolon()
306+
// The only time we can skip the semi-colon is if the config option is set to false
307+
// **and** this is the last expr (even though any following exprs are unreachable)
308+
context.config.trailing_semicolon() || !is_last_expr.unwrap_or(false)
303309
}
304310
_ => true,
305311
},

0 commit comments

Comments
 (0)