Skip to content

Commit b8106eb

Browse files
authored
Merge pull request #2209 from topecongiro/issue-2207
Use an explicit flag to decide on whether to add brace compensation for block expression
2 parents 54f3c21 + f99b775 commit b8106eb

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

src/closures.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ fn rewrite_closure_with_block(
131131
rules: ast::BlockCheckMode::Default,
132132
span: body.span,
133133
};
134-
rewrite_closure_block(&block, prefix, context, shape)
134+
let block = ::expr::rewrite_block_with_visitor(context, "", &block, shape, false)?;
135+
Some(format!("{} {}", prefix, block))
135136
}
136137

137138
// Rewrite closure with a single expression without wrapping its body with block.

src/expr.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn format_expr(
116116
rw
117117
} else {
118118
let prefix = block_prefix(context, block, shape)?;
119-
rewrite_block_with_visitor(context, &prefix, block, shape)
119+
rewrite_block_with_visitor(context, &prefix, block, shape, true)
120120
}
121121
}
122122
ExprType::SubExpression => block.rewrite(context, shape),
@@ -598,11 +598,12 @@ fn rewrite_single_line_block(
598598
None
599599
}
600600

601-
fn rewrite_block_with_visitor(
601+
pub fn rewrite_block_with_visitor(
602602
context: &RewriteContext,
603603
prefix: &str,
604604
block: &ast::Block,
605605
shape: Shape,
606+
has_braces: bool,
606607
) -> Option<String> {
607608
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
608609
return rw;
@@ -620,7 +621,7 @@ fn rewrite_block_with_visitor(
620621
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
621622
}
622623

623-
visitor.visit_block(block, None);
624+
visitor.visit_block(block, None, has_braces);
624625
Some(format!("{}{}", prefix, visitor.buffer))
625626
}
626627

@@ -633,8 +634,9 @@ impl Rewrite for ast::Block {
633634
}
634635

635636
let prefix = block_prefix(context, self, shape)?;
637+
let shape = shape.offset_left(last_line_width(&prefix))?;
636638

637-
let result = rewrite_block_with_visitor(context, &prefix, self, shape);
639+
let result = rewrite_block_with_visitor(context, &prefix, self, shape, true);
638640
if let Some(ref result_str) = result {
639641
if result_str.lines().count() <= 3 {
640642
if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, self, shape) {
@@ -1064,7 +1066,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
10641066
};
10651067
let mut block_context = context.clone();
10661068
block_context.is_if_else_block = self.else_block.is_some();
1067-
let block_str = rewrite_block_with_visitor(&block_context, "", self.block, block_shape)?;
1069+
let block_str =
1070+
rewrite_block_with_visitor(&block_context, "", self.block, block_shape, true)?;
10681071

10691072
let mut result = format!("{}{}", cond_str, block_str);
10701073

src/visitor.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,20 @@ impl<'a> FmtVisitor<'a> {
9090
}
9191
}
9292

93-
pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
93+
pub fn visit_block(
94+
&mut self,
95+
b: &ast::Block,
96+
inner_attrs: Option<&[ast::Attribute]>,
97+
has_braces: bool,
98+
) {
9499
debug!(
95100
"visit_block: {:?} {:?}",
96101
self.codemap.lookup_char_pos(b.span.lo()),
97102
self.codemap.lookup_char_pos(b.span.hi())
98103
);
99104

100105
// Check if this block has braces.
101-
let snippet = self.snippet(b.span);
102-
let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
103-
let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
106+
let brace_compensation = BytePos(if has_braces { 1 } else { 0 });
104107

105108
self.last_pos = self.last_pos + brace_compensation;
106109
self.block_indent = self.block_indent.block_indent(self.config);
@@ -272,7 +275,7 @@ impl<'a> FmtVisitor<'a> {
272275
}
273276

274277
self.last_pos = source!(self, block.span).lo();
275-
self.visit_block(block, inner_attrs)
278+
self.visit_block(block, inner_attrs, true)
276279
}
277280

278281
pub fn visit_item(&mut self, item: &ast::Item) {

tests/source/closure.rs

+6
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,9 @@ fn issue2171() {
198198
}
199199
})
200200
}
201+
202+
fn issue2207() {
203+
a.map(|_| unsafe {
204+
a_very_very_very_very_very_very_very_long_function_name_or_anything_else()
205+
}.to_string())
206+
}

tests/target/closure.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ fn foo() {
123123

124124
fn issue1405() {
125125
open_raw_fd(fd, b'r').and_then(|file| {
126-
Capture::new_raw(None, |_, err| unsafe { raw::pcap_fopen_offline(file, err) })
126+
Capture::new_raw(None, |_, err| unsafe {
127+
raw::pcap_fopen_offline(file, err)
128+
})
127129
});
128130
}
129131

@@ -174,8 +176,9 @@ fn issue1329() {
174176
}
175177

176178
fn issue325() {
177-
let f =
178-
|| unsafe { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx };
179+
let f = || unsafe {
180+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
181+
};
179182
}
180183

181184
fn issue1697() {
@@ -230,3 +233,10 @@ fn issue2171() {
230233
}
231234
})
232235
}
236+
237+
fn issue2207() {
238+
a.map(|_| {
239+
unsafe { a_very_very_very_very_very_very_very_long_function_name_or_anything_else() }
240+
.to_string()
241+
})
242+
}

0 commit comments

Comments
 (0)