fix the visitor's starting position when visiting a labelled block
Close #3217
This commit is contained in:
parent
bbc380b1e6
commit
40174e9481
15
src/expr.rs
15
src/expr.rs
@ -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);
|
||||
|
@ -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;
|
||||
|
8
tests/source/issue-3217.rs
Normal file
8
tests/source/issue-3217.rs
Normal 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 } };
|
||||
}
|
24
tests/target/issue-3217.rs
Normal file
24
tests/target/issue-3217.rs
Normal 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
|
||||
}
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user