Skip to content

Commit 4f233a5

Browse files
authored
Merge pull request #3222 from scampi/issue-3217
fix the visitor's starting position when visiting a labelled block
2 parents bbc380b + 40174e9 commit 4f233a5

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

src/expr.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,16 @@ fn rewrite_empty_block(
417417
prefix: &str,
418418
shape: Shape,
419419
) -> Option<String> {
420+
if !block.stmts.is_empty() {
421+
return None;
422+
}
423+
420424
let label_str = rewrite_label(label);
421425
if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
422426
return None;
423427
}
424428

425-
if block.stmts.is_empty()
426-
&& !block_contains_comment(block, context.source_map)
427-
&& shape.width >= 2
428-
{
429+
if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
429430
return Some(format!("{}{}{{}}", prefix, label_str));
430431
}
431432

@@ -510,13 +511,13 @@ pub fn rewrite_block_with_visitor(
510511
let mut visitor = FmtVisitor::from_context(context);
511512
visitor.block_indent = shape.indent;
512513
visitor.is_if_else_block = context.is_if_else_block();
513-
match block.rules {
514-
ast::BlockCheckMode::Unsafe(..) => {
514+
match (block.rules, label) {
515+
(ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
515516
let snippet = context.snippet(block.span);
516517
let open_pos = snippet.find_uncommented("{")?;
517518
visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
518519
}
519-
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
520+
(ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
520521
}
521522

522523
let inner_attrs = attrs.map(inner_attributes);

src/reorder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
4242
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
4343
// `extern crate foo as bar;`
4444
// ^^^ Comparing this.
45-
let a_orig_name =
46-
a_name.map_or_else(|| a.ident.as_str(), |symbol| symbol.as_str());
47-
let b_orig_name =
48-
b_name.map_or_else(|| b.ident.as_str(), |symbol| symbol.as_str());
45+
let a_orig_name = a_name.map_or_else(|| a.ident.as_str(), |symbol| symbol.as_str());
46+
let b_orig_name = b_name.map_or_else(|| b.ident.as_str(), |symbol| symbol.as_str());
4947
let result = a_orig_name.cmp(&b_orig_name);
5048
if result != Ordering::Equal {
5149
return result;

tests/source/issue-3217.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(label_break_value)]
2+
3+
fn main() {
4+
let mut res = 0;
5+
's_39: { if res == 0i32 { println!("Hello, world!"); } }
6+
's_40: loop { println!("res = {}", res); res += 1; if res == 3i32 { break 's_40; } }
7+
let toto = || { if true { 42 } else { 24 } };
8+
}

tests/target/issue-3217.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(label_break_value)]
2+
3+
fn main() {
4+
let mut res = 0;
5+
's_39: {
6+
if res == 0i32 {
7+
println!("Hello, world!");
8+
}
9+
}
10+
's_40: loop {
11+
println!("res = {}", res);
12+
res += 1;
13+
if res == 3i32 {
14+
break 's_40;
15+
}
16+
}
17+
let toto = || {
18+
if true {
19+
42
20+
} else {
21+
24
22+
}
23+
};
24+
}

0 commit comments

Comments
 (0)