// except according to those terms.
use config::lists::*;
-use syntax::codemap::Span;
use syntax::parse::classify;
+use syntax::source_map::Span;
use syntax::{ast, ptr};
-use codemap::SpanUtils;
use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond, ToExpr};
use items::{span_hi_for_arg, span_lo_for_arg};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
+use source_map::SpanUtils;
use utils::{last_line_width, left_most_sub_expr, stmt_expr};
// This module is pretty messy because of the rules around closures and blocks:
if let ast::ExprKind::Block(ref block, _) = body.node {
// The body of the closure is an empty block.
- if block.stmts.is_empty() && !block_contains_comment(block, context.codemap) {
+ if block.stmts.is_empty() && !block_contains_comment(block, context.source_map) {
return body
.rewrite(context, shape)
.map(|s| format!("{} {}", prefix, s));
fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext) -> bool {
is_unsafe_block(block)
|| block.stmts.len() > 1
- || block_contains_comment(block, context.codemap)
+ || block_contains_comment(block, context.source_map)
|| prefix.contains('\n')
}
+fn veto_block(e: &ast::Expr) -> bool {
+ match e.node {
+ ast::ExprKind::Call(..)
+ | ast::ExprKind::Binary(..)
+ | ast::ExprKind::Cast(..)
+ | ast::ExprKind::Type(..)
+ | ast::ExprKind::Assign(..)
+ | ast::ExprKind::AssignOp(..)
+ | ast::ExprKind::Field(..)
+ | ast::ExprKind::Index(..)
+ | ast::ExprKind::Range(..)
+ | ast::ExprKind::Try(..) => true,
+ _ => false,
+ }
+}
+
// Rewrite closure with a single expression wrapping its body with block.
fn rewrite_closure_with_block(
body: &ast::Expr,
shape: Shape,
) -> Option<String> {
let left_most = left_most_sub_expr(body);
- let veto_block = left_most != body && !classify::expr_requires_semi_to_be_stmt(left_most);
+ let veto_block = veto_block(body) && !classify::expr_requires_semi_to_be_stmt(left_most);
if veto_block {
return None;
}
match expr.node {
ast::ExprKind::Match(..)
| ast::ExprKind::Block(..)
- | ast::ExprKind::Catch(..)
+ | ast::ExprKind::TryBlock(..)
| ast::ExprKind::Loop(..)
| ast::ExprKind::Struct(..) => true,
} else {
Some(rw)
}
- })
- .map(|rw| format!("{} {}", prefix, rw))
+ }).map(|rw| format!("{} {}", prefix, rw))
}
// Rewrite closure whose body is block.
_ => arg_shape,
};
- let fmt = ListFormatting {
- tactic,
- separator: ",",
- trailing_separator: SeparatorTactic::Never,
- separator_place: SeparatorPlace::Back,
- shape: arg_shape,
- ends_with_newline: false,
- preserve_newline: true,
- nested: false,
- config: context.config,
- };
+ let fmt = ListFormatting::new(arg_shape, context.config)
+ .tactic(tactic)
+ .preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str);
let body = match body.node {
ast::ExprKind::Block(ref block, _)
if !is_unsafe_block(block)
- && is_simple_block(block, Some(&body.attrs), context.codemap) =>
+ && is_simple_block(block, Some(&body.attrs), context.source_map) =>
{
stmt_expr(&block.stmts[0]).unwrap_or(body)
}
.map(|e| match e.node {
ast::ExprKind::Closure(..) => true,
_ => false,
- })
- .unwrap_or(false)
- })
- .count() > 1
+ }).unwrap_or(false)
+ }).count()
+ > 1
}
fn is_block_closure_forced(context: &RewriteContext, expr: &ast::Expr) -> bool {