fix the visitor's starting position when visiting a labelled block

Close #3217
This commit is contained in:
Stéphane Campinas 2018-11-28 22:49:09 +01:00
parent bbc380b1e6
commit 40174e9481
No known key found for this signature in database
GPG Key ID: 6D5620D908210133
4 changed files with 42 additions and 11 deletions

View File

@ -417,15 +417,16 @@ fn rewrite_empty_block(
prefix: &str,
shape: Shape,
) -> Option<String> {
if !block.stmts.is_empty() {
return None;
}
let label_str = rewrite_label(label);
if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
return None;
}
if block.stmts.is_empty()
&& !block_contains_comment(block, context.source_map)
&& shape.width >= 2
{
if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
return Some(format!("{}{}{{}}", prefix, label_str));
}
@ -510,13 +511,13 @@ pub fn rewrite_block_with_visitor(
let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = shape.indent;
visitor.is_if_else_block = context.is_if_else_block();
match block.rules {
ast::BlockCheckMode::Unsafe(..) => {
match (block.rules, label) {
(ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
let snippet = context.snippet(block.span);
let open_pos = snippet.find_uncommented("{")?;
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
}
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
}
let inner_attrs = attrs.map(inner_attributes);

View File

@ -42,10 +42,8 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
// `extern crate foo as bar;`
// ^^^ Comparing this.
let a_orig_name =
a_name.map_or_else(|| a.ident.as_str(), |symbol| symbol.as_str());
let b_orig_name =
b_name.map_or_else(|| b.ident.as_str(), |symbol| symbol.as_str());
let a_orig_name = a_name.map_or_else(|| a.ident.as_str(), |symbol| symbol.as_str());
let b_orig_name = b_name.map_or_else(|| b.ident.as_str(), |symbol| symbol.as_str());
let result = a_orig_name.cmp(&b_orig_name);
if result != Ordering::Equal {
return result;

View File

@ -0,0 +1,8 @@
#![feature(label_break_value)]
fn main() {
let mut res = 0;
's_39: { if res == 0i32 { println!("Hello, world!"); } }
's_40: loop { println!("res = {}", res); res += 1; if res == 3i32 { break 's_40; } }
let toto = || { if true { 42 } else { 24 } };
}

View File

@ -0,0 +1,24 @@
#![feature(label_break_value)]
fn main() {
let mut res = 0;
's_39: {
if res == 0i32 {
println!("Hello, world!");
}
}
's_40: loop {
println!("res = {}", res);
res += 1;
if res == 3i32 {
break 's_40;
}
}
let toto = || {
if true {
42
} else {
24
}
};
}