diff --git a/src/expr.rs b/src/expr.rs index 293acf1ea54..aa5913e5f0f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -253,8 +253,16 @@ fn format_expr(expr: &ast::Expr, context.config.max_width(), shape) } - // FIXME(#1537) - ast::ExprKind::Catch(..) => unimplemented!(), + ast::ExprKind::Catch(ref block) => { + if let rewrite @ Some(_) = try_one_line_block(context, shape, "do catch ", block) { + return rewrite; + } + // 9 = `do catch ` + let budget = shape.width.checked_sub(9).unwrap_or(0); + Some(format!("{}{}", + "do catch ", + try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent))))) + } }; match (attr_rw, expr_rw) { (Some(attr_str), Some(expr_str)) => { @@ -268,6 +276,22 @@ fn format_expr(expr: &ast::Expr, } } +fn try_one_line_block(context: &RewriteContext, + shape: Shape, + prefix: &str, + block: &ast::Block) + -> Option { + if is_simple_block(block, context.codemap) { + let expr_shape = Shape::legacy(shape.width - prefix.len(), shape.indent); + let expr_str = try_opt!(block.stmts[0].rewrite(context, expr_shape)); + let result = format!("{}{{ {} }}", prefix, expr_str); + if result.len() <= shape.width && !result.contains('\n') { + return Some(result); + } + } + None +} + pub fn rewrite_pair(lhs: &LHS, rhs: &RHS, prefix: &str, @@ -622,9 +646,7 @@ fn rewrite_closure(capture: ast::CaptureBy, // means we must re-format. let block_shape = shape.block().with_max_width(context.config); let block_str = try_opt!(block.rewrite(&context, block_shape)); - Some(format!("{} {}", - prefix, - try_opt!(block_str.rewrite(context, block_shape)))) + Some(format!("{} {}", prefix, block_str)) } } @@ -689,24 +711,13 @@ impl Rewrite for ast::Block { } else { "unsafe ".to_owned() }; - - if is_simple_block(self, context.codemap) && prefix.len() < shape.width { - let expr_str = - self.stmts[0].rewrite(context, - Shape::legacy(shape.width - prefix.len(), - shape.indent)); - let expr_str = try_opt!(expr_str); - let result = format!("{}{{ {} }}", prefix, expr_str); - if result.len() <= shape.width && !result.contains('\n') { - return Some(result); - } + if let result @ Some(_) = try_one_line_block(context, shape, &prefix, self) { + return result; } - prefix } ast::BlockCheckMode::Default => { visitor.last_pos = self.span.lo; - String::new() } }; diff --git a/tests/source/catch.rs b/tests/source/catch.rs new file mode 100644 index 00000000000..64cc9e7a207 --- /dev/null +++ b/tests/source/catch.rs @@ -0,0 +1,27 @@ +#![feature(catch_expr)] + +fn main() { + let x = do catch { + foo()? + }; + + let x = do catch /* Invisible comment */ { foo()? }; + + let x = do catch { + unsafe { foo()? } + }; + + let y = match (do catch { + foo()? + }) { + _ => (), + }; + + do catch { + foo()?; + }; + + do catch { + // Regular do catch block + }; +} diff --git a/tests/target/catch.rs b/tests/target/catch.rs new file mode 100644 index 00000000000..640f9bade49 --- /dev/null +++ b/tests/target/catch.rs @@ -0,0 +1,21 @@ +#![feature(catch_expr)] + +fn main() { + let x = do catch { foo()? }; + + let x = do catch /* Invisible comment */ { foo()? }; + + let x = do catch { unsafe { foo()? } }; + + let y = match (do catch { foo()? }) { + _ => (), + }; + + do catch { + foo()?; + }; + + do catch { + // Regular do catch block + }; +} diff --git a/tests/target/closure.rs b/tests/target/closure.rs index d51c76b9e48..3d20102bef0 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure.rs @@ -109,8 +109,10 @@ fn foo() { fn issue1405() { open_raw_fd(fd, b'r').and_then(|file| { - Capture::new_raw(None, |_, err| unsafe { raw::pcap_fopen_offline(file, err) }) - }); + Capture::new_raw(None, |_, err| unsafe { + raw::pcap_fopen_offline(file, err) + }) + }); } fn issue1466() { diff --git a/tests/target/match.rs b/tests/target/match.rs index 09df7ebd55f..6acab043e67 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -326,11 +326,10 @@ fn issue1371() { sfEvtGainedFocus => GainedFocus, sfEvtTextEntered => { TextEntered { - unicode: - unsafe { - ::std::char::from_u32((*event.text.as_ref()).unicode) - .expect("Invalid unicode encountered on TextEntered event") - }, + unicode: unsafe { + ::std::char::from_u32((*event.text.as_ref()).unicode) + .expect("Invalid unicode encountered on TextEntered event") + }, } } sfEvtKeyPressed => {