Use format_expr wherever single-lined block is not allowed

This commit is contained in:
topecongiro 2017-06-20 21:35:52 +09:00
parent eeb20e2033
commit fb1225a8af
3 changed files with 50 additions and 10 deletions

View file

@ -42,7 +42,7 @@ impl Rewrite for ast::Expr {
}
#[derive(PartialEq)]
enum ExprType {
pub enum ExprType {
Statement,
SubExpression,
}
@ -67,7 +67,7 @@ fn combine_attr_and_expr(
format!("{}{}{}", attr_str, separator, expr_str)
}
fn format_expr(
pub fn format_expr(
expr: &ast::Expr,
expr_type: ExprType,
context: &RewriteContext,
@ -160,7 +160,23 @@ fn format_expr(
to_control_flow(expr, expr_type)
.and_then(|control_flow| control_flow.rewrite(context, shape))
}
ast::ExprKind::Block(ref block) => block.rewrite(context, shape),
ast::ExprKind::Block(ref block) => {
match expr_type {
ExprType::Statement => {
if is_unsafe_block(block) {
block.rewrite(context, shape)
} else {
// Rewrite block without trying to put it in a single line.
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
return rw;
}
let prefix = try_opt!(block_prefix(context, block, shape));
rewrite_block_with_visitor(context, &prefix, block, shape)
}
}
ExprType::SubExpression => block.rewrite(context, shape),
}
}
ast::ExprKind::Match(ref cond, ref arms) => {
rewrite_match(context, cond, arms, shape, expr.span)
}
@ -1280,7 +1296,12 @@ impl<'a> Rewrite for ControlFlow<'a> {
};
let mut block_context = context.clone();
block_context.is_if_else_block = self.else_block.is_some();
let block_str = try_opt!(self.block.rewrite(&block_context, block_shape));
let block_str = try_opt!(rewrite_block_with_visitor(
&block_context,
"",
self.block,
block_shape,
));
let mut result = format!("{}{}", cond_str, block_str);
@ -1322,7 +1343,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
width: min(1, shape.width),
..shape
};
else_block.rewrite(context, else_shape)
format_expr(else_block, ExprType::Statement, context, else_shape)
}
};
@ -1689,7 +1710,10 @@ impl Rewrite for ast::Arm {
.unwrap()
.sub_width(comma.len())
.unwrap();
let rewrite = nop_block_collapse(body.rewrite(context, arm_shape), arm_shape.width);
let rewrite = nop_block_collapse(
format_expr(body, ExprType::Statement, context, arm_shape),
arm_shape.width,
);
let is_block = if let ast::ExprKind::Block(..) = body.node {
true
} else {
@ -1724,7 +1748,7 @@ impl Rewrite for ast::Arm {
// necessary.
let body_shape = try_opt!(shape.block_left(context.config.tab_spaces()));
let next_line_body = try_opt!(nop_block_collapse(
body.rewrite(context, body_shape),
format_expr(body, ExprType::Statement, context, body_shape),
body_shape.width,
));
let indent_str = shape

View file

@ -17,7 +17,7 @@ use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wr
trimmed_last_line_width, colon_spaces, mk_sp};
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, list_helper,
DefinitiveListTactic, ListTactic, definitive_tactic};
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs};
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, ExprType};
use comment::{FindUncommented, contains_comment, rewrite_comment, recover_comment_removed};
use visitor::FmtVisitor;
use rewrite::{Rewrite, RewriteContext};
@ -352,7 +352,9 @@ impl<'a> FmtVisitor<'a> {
Some(e) => {
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
e.rewrite(
format_expr(
&e,
ExprType::Statement,
&self.get_context(),
Shape::indented(self.block_indent, self.config),
).map(|s| s + suffix)

View file

@ -17,6 +17,7 @@ use syntax::parse::ParseSess;
use strings::string_buffer::StringBuffer;
use {Indent, Shape};
use expr::{format_expr, ExprType};
use utils::{self, mk_sp};
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, FindUncommented};
@ -87,7 +88,20 @@ impl<'a> FmtVisitor<'a> {
);
self.push_rewrite(stmt.span, rewrite);
}
ast::StmtKind::Expr(ref expr) |
ast::StmtKind::Expr(ref expr) => {
let rewrite = format_expr(
expr,
ExprType::Statement,
&self.get_context(),
Shape::indented(self.block_indent, self.config),
);
let span = if expr.attrs.is_empty() {
stmt.span
} else {
mk_sp(expr.attrs[0].span.lo, stmt.span.hi)
};
self.push_rewrite(span, rewrite)
}
ast::StmtKind::Semi(ref expr) => {
let rewrite = stmt.rewrite(
&self.get_context(),