Merge pull request #2209 from topecongiro/issue-2207

Use an explicit flag to decide on whether to add brace compensation for block expression
This commit is contained in:
Nick Cameron 2017-11-30 10:54:25 +13:00 committed by GitHub
commit b8106eb2aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 14 deletions

View File

@ -131,7 +131,8 @@ fn rewrite_closure_with_block(
rules: ast::BlockCheckMode::Default,
span: body.span,
};
rewrite_closure_block(&block, prefix, context, shape)
let block = ::expr::rewrite_block_with_visitor(context, "", &block, shape, false)?;
Some(format!("{} {}", prefix, block))
}
// Rewrite closure with a single expression without wrapping its body with block.

View File

@ -116,7 +116,7 @@ pub fn format_expr(
rw
} else {
let prefix = block_prefix(context, block, shape)?;
rewrite_block_with_visitor(context, &prefix, block, shape)
rewrite_block_with_visitor(context, &prefix, block, shape, true)
}
}
ExprType::SubExpression => block.rewrite(context, shape),
@ -598,11 +598,12 @@ fn rewrite_single_line_block(
None
}
fn rewrite_block_with_visitor(
pub fn rewrite_block_with_visitor(
context: &RewriteContext,
prefix: &str,
block: &ast::Block,
shape: Shape,
has_braces: bool,
) -> Option<String> {
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
return rw;
@ -620,7 +621,7 @@ fn rewrite_block_with_visitor(
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
}
visitor.visit_block(block, None);
visitor.visit_block(block, None, has_braces);
Some(format!("{}{}", prefix, visitor.buffer))
}
@ -633,8 +634,9 @@ impl Rewrite for ast::Block {
}
let prefix = block_prefix(context, self, shape)?;
let shape = shape.offset_left(last_line_width(&prefix))?;
let result = rewrite_block_with_visitor(context, &prefix, self, shape);
let result = rewrite_block_with_visitor(context, &prefix, self, shape, true);
if let Some(ref result_str) = result {
if result_str.lines().count() <= 3 {
if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, self, shape) {
@ -1064,7 +1066,8 @@ 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 = rewrite_block_with_visitor(&block_context, "", self.block, block_shape)?;
let block_str =
rewrite_block_with_visitor(&block_context, "", self.block, block_shape, true)?;
let mut result = format!("{}{}", cond_str, block_str);

View File

@ -90,7 +90,12 @@ impl<'a> FmtVisitor<'a> {
}
}
pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
pub fn visit_block(
&mut self,
b: &ast::Block,
inner_attrs: Option<&[ast::Attribute]>,
has_braces: bool,
) {
debug!(
"visit_block: {:?} {:?}",
self.codemap.lookup_char_pos(b.span.lo()),
@ -98,9 +103,7 @@ impl<'a> FmtVisitor<'a> {
);
// Check if this block has braces.
let snippet = self.snippet(b.span);
let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
let brace_compensation = BytePos(if has_braces { 1 } else { 0 });
self.last_pos = self.last_pos + brace_compensation;
self.block_indent = self.block_indent.block_indent(self.config);
@ -272,7 +275,7 @@ impl<'a> FmtVisitor<'a> {
}
self.last_pos = source!(self, block.span).lo();
self.visit_block(block, inner_attrs)
self.visit_block(block, inner_attrs, true)
}
pub fn visit_item(&mut self, item: &ast::Item) {

View File

@ -198,3 +198,9 @@ fn issue2171() {
}
})
}
fn issue2207() {
a.map(|_| unsafe {
a_very_very_very_very_very_very_very_long_function_name_or_anything_else()
}.to_string())
}

View File

@ -123,7 +123,9 @@ fn foo() {
fn issue1405() {
open_raw_fd(fd, b'r').and_then(|file| {
Capture::new_raw(None, |_, err| unsafe { raw::pcap_fopen_offline(file, err) })
Capture::new_raw(None, |_, err| unsafe {
raw::pcap_fopen_offline(file, err)
})
});
}
@ -174,8 +176,9 @@ fn issue1329() {
}
fn issue325() {
let f =
|| unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
let f = || unsafe {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
};
}
fn issue1697() {
@ -230,3 +233,10 @@ fn issue2171() {
}
})
}
fn issue2207() {
a.map(|_| {
unsafe { a_very_very_very_very_very_very_very_long_function_name_or_anything_else() }
.to_string()
})
}