diff --git a/src/expr.rs b/src/expr.rs index 973c72d871f..5d0383ddcb2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -417,15 +417,16 @@ fn rewrite_empty_block( prefix: &str, shape: Shape, ) -> Option { + 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); diff --git a/src/reorder.rs b/src/reorder.rs index 89bafccd6ed..eddc0446035 100644 --- a/src/reorder.rs +++ b/src/reorder.rs @@ -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; diff --git a/tests/source/issue-3217.rs b/tests/source/issue-3217.rs new file mode 100644 index 00000000000..176c702002a --- /dev/null +++ b/tests/source/issue-3217.rs @@ -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 } }; +} diff --git a/tests/target/issue-3217.rs b/tests/target/issue-3217.rs new file mode 100644 index 00000000000..5121320a097 --- /dev/null +++ b/tests/target/issue-3217.rs @@ -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 + } + }; +}