Skip to content

Commit d9a0992

Browse files
CosmicHorrorDevcalebcartwright
authored andcommitted
Switch ast::Stmt rewriting use to stmt::Stmt::from_simple_block
1 parent 8850854 commit d9a0992

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

src/expr.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
2626
use crate::shape::{Indent, Shape};
2727
use crate::source_map::{LineRangeUtils, SpanUtils};
2828
use crate::spanned::Spanned;
29+
use crate::stmt;
2930
use crate::string::{rewrite_string, StringFormat};
3031
use crate::types::{rewrite_path, PathContext};
3132
use crate::utils::{
@@ -515,9 +516,9 @@ fn rewrite_single_line_block(
515516
label: Option<ast::Label>,
516517
shape: Shape,
517518
) -> Option<String> {
518-
if is_simple_block(context, block, attrs) {
519+
if let Some(block_expr) = stmt::Stmt::from_simple_block(context, block, attrs) {
519520
let expr_shape = shape.offset_left(last_line_width(prefix))?;
520-
let expr_str = block.stmts[0].rewrite(context, expr_shape)?;
521+
let expr_str = block_expr.rewrite(context, expr_shape)?;
521522
let label_str = rewrite_label(label);
522523
let result = format!("{}{}{{ {} }}", prefix, label_str, expr_str);
523524
if result.len() <= shape.width && !result.contains('\n') {
@@ -799,19 +800,19 @@ impl<'a> ControlFlow<'a> {
799800
let fixed_cost = self.keyword.len() + " { } else { }".len();
800801

801802
if let ast::ExprKind::Block(ref else_node, _) = else_block.kind {
802-
if !is_simple_block(context, self.block, None)
803-
|| !is_simple_block(context, else_node, None)
804-
|| pat_expr_str.contains('\n')
805-
{
806-
return None;
807-
}
803+
let (if_expr, else_expr) = match (
804+
stmt::Stmt::from_simple_block(context, self.block, None),
805+
stmt::Stmt::from_simple_block(context, else_node, None),
806+
pat_expr_str.contains('\n'),
807+
) {
808+
(Some(if_expr), Some(else_expr), false) => (if_expr, else_expr),
809+
_ => return None,
810+
};
808811

809812
let new_width = width.checked_sub(pat_expr_str.len() + fixed_cost)?;
810-
let expr = &self.block.stmts[0];
811-
let if_str = expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
813+
let if_str = if_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
812814

813815
let new_width = new_width.checked_sub(if_str.len())?;
814-
let else_expr = &else_node.stmts[0];
815816
let else_str = else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty()))?;
816817

817818
if if_str.contains('\n') || else_str.contains('\n') {

src/stmt.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_span::Span;
33

44
use crate::comment::recover_comment_removed;
55
use crate::config::Version;
6-
use crate::expr::{format_expr, ExprType};
6+
use crate::expr::{format_expr, is_simple_block, ExprType};
77
use crate::rewrite::{Rewrite, RewriteContext};
88
use crate::shape::Shape;
99
use crate::source_map::LineRangeUtils;
@@ -33,6 +33,21 @@ impl<'a> Stmt<'a> {
3333
}
3434
}
3535

36+
pub(crate) fn from_simple_block(
37+
context: &RewriteContext<'_>,
38+
block: &'a ast::Block,
39+
attrs: Option<&[ast::Attribute]>,
40+
) -> Option<Self> {
41+
if is_simple_block(context, block, attrs) {
42+
let inner = &block.stmts[0];
43+
// Simple blocks only contain one expr and no stmts
44+
let is_last = true;
45+
Some(Stmt { inner, is_last })
46+
} else {
47+
None
48+
}
49+
}
50+
3651
pub(crate) fn from_ast_node(inner: &'a ast::Stmt, is_last: bool) -> Self {
3752
Stmt { inner, is_last }
3853
}
@@ -85,23 +100,17 @@ impl<'a> Rewrite for Stmt<'a> {
85100
shape,
86101
self.as_ast_node(),
87102
expr_type,
88-
Some(self.is_last_expr()),
103+
self.is_last_expr(),
89104
)
90105
}
91106
}
92107

93-
impl Rewrite for ast::Stmt {
94-
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
95-
format_stmt(context, shape, self, ExprType::Statement, None)
96-
}
97-
}
98-
99108
fn format_stmt(
100109
context: &RewriteContext<'_>,
101110
shape: Shape,
102111
stmt: &ast::Stmt,
103112
expr_type: ExprType,
104-
is_last_expr: Option<bool>,
113+
is_last_expr: bool,
105114
) -> Option<String> {
106115
skip_out_of_file_lines_range!(context, stmt.span());
107116

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub(crate) fn semicolon_for_expr(context: &RewriteContext<'_>, expr: &ast::Expr)
295295
pub(crate) fn semicolon_for_stmt(
296296
context: &RewriteContext<'_>,
297297
stmt: &ast::Stmt,
298-
is_last_expr: Option<bool>,
298+
is_last_expr: bool,
299299
) -> bool {
300300
match stmt.kind {
301301
ast::StmtKind::Semi(ref expr) => match expr.kind {
@@ -305,7 +305,7 @@ pub(crate) fn semicolon_for_stmt(
305305
ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => {
306306
// The only time we can skip the semi-colon is if the config option is set to false
307307
// **and** this is the last expr (even though any following exprs are unreachable)
308-
context.config.trailing_semicolon() || !is_last_expr.unwrap_or(false)
308+
context.config.trailing_semicolon() || !is_last_expr
309309
}
310310
_ => true,
311311
},

0 commit comments

Comments
 (0)