Skip to content

Commit d157383

Browse files
committed
Visit last if_stmt
1 parent 07c7447 commit d157383

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/expr.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,33 @@ impl Rewrite for ast::Stmt {
545545
};
546546

547547
let shape = shape.sub_width(suffix.len())?;
548-
let expr_type =
549-
if stmt_is_expr(&self) && context.snippet(self.span).starts_with("if ") {
550-
ExprType::SubExpression
551-
} else {
552-
ExprType::Statement
553-
};
554-
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
548+
format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
549+
}
550+
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
551+
};
552+
result.and_then(|res| recover_comment_removed(res, self.span(), context))
553+
}
554+
}
555+
556+
pub(crate) trait RewriteLastStmt {
557+
fn rewrite_last_stmt(&self, context: &RewriteContext, shape: Shape) -> Option<String>;
558+
}
559+
560+
impl RewriteLastStmt for ast::Stmt {
561+
fn rewrite_last_stmt(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
562+
skip_out_of_file_lines_range!(context, self.span());
563+
564+
let result = match self.node {
565+
ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
566+
ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
567+
let suffix = if semicolon_for_stmt(context, self) {
568+
";"
569+
} else {
570+
""
571+
};
572+
573+
let shape = shape.sub_width(suffix.len())?;
574+
format_expr(ex, ExprType::SubExpression, context, shape).map(|s| s + suffix)
555575
}
556576
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
557577
};
@@ -1136,26 +1156,16 @@ pub fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
11361156
}
11371157
}
11381158

1139-
fn stmt_is_if(stmt: &ast::Stmt) -> bool {
1159+
pub(crate) fn stmt_is_if(stmt: &ast::Stmt) -> bool {
11401160
match stmt.node {
1141-
ast::StmtKind::Semi(ref e) | ast::StmtKind::Expr(ref e) => {
1142-
match e.node {
1143-
ast::ExprKind::If(..) => true,
1144-
_ => false,
1145-
}
1161+
ast::StmtKind::Semi(ref e) | ast::StmtKind::Expr(ref e) => match e.node {
1162+
ast::ExprKind::If(..) => true,
1163+
_ => false,
11461164
},
11471165
_ => false,
11481166
}
11491167
}
11501168

1151-
fn block_last_stmt_is_if(block: &ast::Block) -> bool {
1152-
if let Some(ref stmt) = block.stmts.last() {
1153-
stmt_is_if(stmt)
1154-
} else {
1155-
false
1156-
}
1157-
}
1158-
11591169
pub fn is_unsafe_block(block: &ast::Block) -> bool {
11601170
if let ast::BlockCheckMode::Unsafe(..) = block.rules {
11611171
true

src/visitor.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use attr::*;
1717
use codemap::{LineRangeUtils, SpanUtils};
1818
use comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
1919
use config::{BraceStyle, Config};
20+
use expr::{stmt_is_if, RewriteLastStmt};
2021
use items::{
2122
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
2223
rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
@@ -79,7 +80,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
7980
Shape::indented(self.block_indent, self.config)
8081
}
8182

82-
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
83+
fn visit_stmt(&mut self, stmt: &ast::Stmt, if_is_last_stmt: bool) {
8384
debug!(
8485
"visit_stmt: {:?} {:?}",
8586
self.codemap.lookup_char_pos(stmt.span.lo()),
@@ -94,7 +95,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
9495
if contains_skip(get_attrs_from_stmt(stmt)) {
9596
self.push_skipped_with_span(stmt.span());
9697
} else {
97-
let rewrite = stmt.rewrite(&self.get_context(), self.shape());
98+
let rewrite = if if_is_last_stmt {
99+
stmt.rewrite_last_stmt(&self.get_context(), self.shape())
100+
} else {
101+
stmt.rewrite(&self.get_context(), self.shape())
102+
};
98103
self.push_rewrite(stmt.span(), rewrite)
99104
}
100105
}
@@ -662,7 +667,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
662667
.collect();
663668

664669
if items.is_empty() {
665-
self.visit_stmt(&stmts[0]);
670+
let if_is_last_stmt = stmts[0] == stmts[stmts.len() - 1] && stmt_is_if(&stmts[0]);
671+
self.visit_stmt(&stmts[0], if_is_last_stmt);
666672
self.walk_stmts(&stmts[1..]);
667673
} else {
668674
self.visit_items_with_reordering(&items);

0 commit comments

Comments
 (0)