// Total of all items excluding the last.
let last_non_try_index = rewrites.len() - (1 + trailing_try_num);
- let almost_total = rewrites[..last_non_try_index].iter().fold(
- 0,
- |a, b| a + first_line_width(b),
- ) + parent_rewrite.len();
+ let almost_total = rewrites[..last_non_try_index]
+ .iter()
+ .fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
let one_line_len = rewrites.iter().fold(0, |a, r| a + first_line_width(r)) +
parent_rewrite.len();
fn custom_opener(s: &str) -> &str {
s.lines().next().map_or("", |first_line| {
- first_line.find(' ').map_or(
- first_line,
- |space_index| &first_line[0..space_index + 1],
- )
+ first_line
+ .find(' ')
+ .map_or(first_line, |space_index| &first_line[0..space_index + 1])
})
}
let new_width = try_opt!(new_width.checked_sub(if_str.len()));
let else_expr = &else_node.stmts[0];
- let else_str = try_opt!(else_expr.rewrite(
- context,
- Shape::legacy(new_width, Indent::empty()),
- ));
+ let else_str =
+ try_opt!(else_expr.rewrite(context, Shape::legacy(new_width, Indent::empty())));
if if_str.contains('\n') || else_str.contains('\n') {
return None;
let between_kwd_else_block = mk_sp(
self.block.span.hi,
- context.codemap.span_before(
- mk_sp(self.block.span.hi, else_block.span.lo),
- "else",
- ),
+ context
+ .codemap
+ .span_before(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
);
let between_kwd_else_block_comment =
extract_comment(between_kwd_else_block, context, shape);
let after_else = mk_sp(
- context.codemap.span_after(
- mk_sp(self.block.span.hi, else_block.span.lo),
- "else",
- ),
+ context
+ .codemap
+ .span_after(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
else_block.span.lo,
);
let after_else_comment = extract_comment(after_else, context, shape);
write!(
&mut result,
"{}else{}",
- between_kwd_else_block_comment.as_ref().map_or(
- between_sep,
- |s| &**s,
- ),
+ between_kwd_else_block_comment
+ .as_ref()
+ .map_or(between_sep, |s| &**s),
after_else_comment.as_ref().map_or(after_sep, |s| &**s)
).ok()
);
let arm_indent_str = arm_shape.indent.to_string(context.config);
- let open_brace_pos = context.codemap.span_after(
- mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
- "{",
- );
+ let open_brace_pos = context
+ .codemap
+ .span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{");
for (i, arm) in arms.iter().enumerate() {
// Make sure we get the stuff between arms.
fn need_block_indent(s: &str, shape: Shape) -> bool {
s.lines().skip(1).any(|s| {
- s.find(|c| !char::is_whitespace(c)).map_or(
- false,
- |w| w + 1 < shape.indent.width(),
- )
+ s.find(|c| !char::is_whitespace(c))
+ .map_or(false, |w| w + 1 < shape.indent.width())
})
}
where
T: Rewrite + Spanned + ToExpr + 'a,
{
- args.last().map_or(
- false,
- |x| x.can_be_overflowed(context, args.len()),
- )
+ args.last()
+ .map_or(false, |x| x.can_be_overflowed(context, args.len()))
}
pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: usize) -> bool {
Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
None => {
let expr_offset = shape.indent.block_indent(context.config);
- let expr = field.expr.rewrite(
- context,
- Shape::indented(expr_offset, context.config),
- );
+ let expr = field
+ .expr
+ .rewrite(context, Shape::indented(expr_offset, context.config));
expr.map(|s| {
format!(
"{}{}:\n{}{}",
0
};
// 1 = space between operator and rhs.
- let orig_shape = try_opt!(shape.block_indent(0).offset_left(last_line_width + 1));
+ let orig_shape = try_opt!(shape.offset_left(last_line_width + 1));
let rhs = match ex.node {
ast::ExprKind::Mac(ref mac) => {
match rewrite_macro(mac, None, context, orig_shape, MacroPosition::Expression) {
let context = self.get_context();
let indent = self.block_indent;
- let mut result = try_opt!(field.node.attrs.rewrite(
- &context,
- Shape::indented(indent, self.config),
- ));
+ let mut result = try_opt!(
+ field
+ .node
+ .attrs
+ .rewrite(&context, Shape::indented(indent, self.config))
+ );
if !result.is_empty() {
let shape = Shape {
width: context.config.max_width(),
let name = self.ident;
let vis = format_visibility(&self.vis);
- let mut attr_str = try_opt!(self.attrs.rewrite(
- context,
- Shape::indented(shape.indent, context.config),
- ));
+ let mut attr_str = try_opt!(
+ self.attrs
+ .rewrite(context, Shape::indented(shape.indent, context.config))
+ );
// Try format missing comments after attributes
let missing_comment = if !self.attrs.is_empty() {
rewrite_missing_comment_on_field(
let type_offset = shape.indent.block_indent(context.config);
let rewrite_type_in_next_line = || {
- self.ty.rewrite(
- context,
- Shape::indented(type_offset, context.config),
- )
+ self.ty
+ .rewrite(context, Shape::indented(type_offset, context.config))
};
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
impl Rewrite for ast::Arg {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
if is_named_arg(self) {
- let mut result = try_opt!(self.pat.rewrite(
- context,
- Shape::legacy(shape.width, shape.indent),
- ));
+ let mut result = try_opt!(
+ self.pat
+ .rewrite(context, Shape::legacy(shape.width, shape.indent))
+ );
if !is_empty_infer(context, &*self.ty) {
if context.config.space_before_type_annotation() {
let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
result.push_str(&generics_str);
- let snuggle_angle_bracket = generics_str.lines().last().map_or(
- false,
- |l| l.trim_left().len() == 1,
- );
+ let snuggle_angle_bracket = generics_str
+ .lines()
+ .last()
+ .map_or(false, |l| l.trim_left().len() == 1);
// Note that the width and indent don't really matter, we'll re-layout the
// return type later anyway.
- let ret_str = try_opt!(fd.output.rewrite(
- &context,
- Shape::indented(indent, context.config),
- ));
+ let ret_str = try_opt!(
+ fd.output
+ .rewrite(&context, Shape::indented(indent, context.config))
+ );
let multi_line_ret_str = ret_str.contains('\n');
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
}
// A conservative estimation, to goal is to be over all parens in generics
- let args_start = generics.ty_params.last().map_or(
- span.lo,
- |tp| end_typaram(tp),
- );
+ let args_start = generics
+ .ty_params
+ .last()
+ .map_or(span.lo, |tp| end_typaram(tp));
let args_span = mk_sp(
context.codemap.span_after(mk_sp(args_start, span.hi), "("),
span_for_return(&fd.output).lo,
}
// If the last line of args contains comment, we cannot put the closing paren
// on the same line.
- if arg_str.lines().last().map_or(
- false,
- |last_line| last_line.contains("//"),
- )
+ if arg_str
+ .lines()
+ .last()
+ .map_or(false, |last_line| last_line.contains("//"))
{
args_last_line_contains_comment = true;
result.push('\n');
if multi_line_ret_str || ret_should_indent {
// Now that we know the proper indent and width, we need to
// re-layout the return type.
- let ret_str = try_opt!(fd.output.rewrite(
- context,
- Shape::indented(ret_indent, context.config),
- ));
+ let ret_str = try_opt!(
+ fd.output
+ .rewrite(context, Shape::indented(ret_indent, context.config))
+ );
result.push_str(&ret_str);
} else {
result.push_str(&ret_str);
let snippet_hi = span.hi;
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
// Try to preserve the layout of the original snippet.
- let original_starts_with_newline = snippet.find(|c| c != ' ').map_or(
- false,
- |i| snippet[i..].starts_with('\n'),
- );
- let original_ends_with_newline = snippet.rfind(|c| c != ' ').map_or(
- false,
- |i| snippet[i..].ends_with('\n'),
- );
+ let original_starts_with_newline = snippet
+ .find(|c| c != ' ')
+ .map_or(false, |i| snippet[i..].starts_with('\n'));
+ let original_ends_with_newline = snippet
+ .rfind(|c| c != ' ')
+ .map_or(false, |i| snippet[i..].ends_with('\n'));
let snippet = snippet.trim();
if !snippet.is_empty() {
result.push(if original_starts_with_newline {
impl ListItem {
pub fn is_multiline(&self) -> bool {
self.item.as_ref().map_or(false, |s| s.contains('\n')) || self.pre_comment.is_some() ||
- self.post_comment.as_ref().map_or(
- false,
- |s| s.contains('\n'),
- )
+ self.post_comment
+ .as_ref()
+ .map_or(false, |s| s.contains('\n'))
}
pub fn has_line_pre_comment(&self) -> bool {
- self.pre_comment.as_ref().map_or(
- false,
- |comment| comment.starts_with("//"),
- )
+ self.pre_comment
+ .as_ref()
+ .map_or(false, |comment| comment.starts_with("//"))
}
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
let rewrite = try_opt!(rewrite_array(
expr_vec.iter().map(|x| &**x),
mk_sp(
- context.codemap.span_after(
- mac.span,
- original_style.opener(),
- ),
+ context
+ .codemap
+ .span_after(mac.span, original_style.opener()),
mac.span.hi - BytePos(1),
),
context,
let subslice_num_lines = subslice.chars().filter(|c| *c == '\n').count();
if rewrite_next_comment &&
- !self.config.file_lines().intersects_range(
- file_name,
- cur_line,
- cur_line + subslice_num_lines,
- )
+ !self.config
+ .file_lines()
+ .intersects_range(file_name, cur_line, cur_line + subslice_num_lines)
{
rewrite_next_comment = false;
}
TypeDensity::Compressed => format!("{}=", binding.ident),
};
let budget = try_opt!(shape.width.checked_sub(result.len()));
- let rewrite = try_opt!(binding.ty.rewrite(
- context,
- Shape::legacy(budget, shape.indent + result.len()),
- ));
+ let rewrite = try_opt!(
+ binding
+ .ty
+ .rewrite(context, Shape::legacy(budget, shape.indent + result.len()))
+ );
result.push_str(&rewrite);
Some(result)
}
// 3 = " = ".len()
let used_width = 3 + lhs_ty_str.len();
let budget = try_opt!(shape.width.checked_sub(used_width));
- let rhs_ty_str = try_opt!(rhs_ty.rewrite(
- context,
- Shape::legacy(budget, shape.indent + used_width),
- ));
+ let rhs_ty_str = try_opt!(
+ rhs_ty.rewrite(context, Shape::legacy(budget, shape.indent + used_width))
+ );
format!("{} = {}", lhs_ty_str, rhs_ty_str)
}
};
format!(
"&{}{}",
mut_str,
- try_opt!(mt.ty.rewrite(
- context,
- Shape::legacy(budget, shape.indent + 1 + mut_len),
- ))
+ try_opt!(
+ mt.ty.rewrite(
+ context,
+ Shape::legacy(budget, shape.indent + 1 + mut_len),
+ )
+ )
)
}
})
ast::ItemKind::Impl(..) => {
self.format_missing_with_indent(source!(self, item.span).lo);
let snippet = self.get_context().snippet(item.span);
- let where_span_end =
- snippet
- .find_uncommented("{")
- .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
+ let where_span_end = snippet
+ .find_uncommented("{")
+ .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
if let Some(impl_str) = format_impl(
&self.get_context(),
item,