Skip to content

Use an explicit flag to decide on whether to add brace compensation for block expression #2209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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))
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 8 additions & 5 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,20 @@ 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()),
self.codemap.lookup_char_pos(b.span.hi())
);

// 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);
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions tests/source/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
16 changes: 13 additions & 3 deletions tests/target/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
});
}

Expand Down Expand Up @@ -174,8 +176,9 @@ fn issue1329() {
}

fn issue325() {
let f =
|| unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
let f = || unsafe {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
};
}

fn issue1697() {
Expand Down Expand Up @@ -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()
})
}