From: Otavio Salvador Date: Mon, 8 Oct 2018 23:49:33 +0000 (-0300) Subject: Only combine `match` if its condition expression fits in a single line X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=8feeddf1f88cb3753b51c894cffea4f11b0036d3;hp=8b709c0019244d8554c0799084815146a5ce800a;p=rust.git Only combine `match` if its condition expression fits in a single line This improves the formatting and reading of code avoiding the condition expression to be rewrite, if it goes multi line. Fixes: #3029. Signed-off-by: Otavio Salvador --- diff --git a/src/overflow.rs b/src/overflow.rs index a7fc9728666..40cfc4be30f 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -18,6 +18,7 @@ use closures; use expr::{ can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr, + rewrite_cond, }; use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator}; use macros::MacroArg; @@ -403,6 +404,16 @@ fn rewrite_last_item_with_overflow( closures::rewrite_last_closure(self.context, expr, shape) } } + ast::ExprKind::Match(..) => { + let multi_line = rewrite_cond(self.context, expr, shape) + .map_or(false, |cond| cond.contains('\n')); + + if multi_line { + None + } else { + expr.rewrite(self.context, shape) + } + } _ => expr.rewrite(self.context, shape), } } diff --git a/tests/source/issue-3029.rs b/tests/source/issue-3029.rs new file mode 100644 index 00000000000..49ce07fae3a --- /dev/null +++ b/tests/source/issue-3029.rs @@ -0,0 +1,21 @@ +fn foo() { + EvaluateJSReply::NumberValue( + match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) { + Ok(ConversionResult::Success(v)) => v, + _ => unreachable!(), + }, + ) +} + +fn bar() { + { + { + EvaluateJSReply::NumberValue( + match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) { + Ok(ConversionResult::Success(v)) => v, + _ => unreachable!(), + }, + ) + } + } +} diff --git a/tests/target/issue-3029.rs b/tests/target/issue-3029.rs new file mode 100644 index 00000000000..49ce07fae3a --- /dev/null +++ b/tests/target/issue-3029.rs @@ -0,0 +1,21 @@ +fn foo() { + EvaluateJSReply::NumberValue( + match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) { + Ok(ConversionResult::Success(v)) => v, + _ => unreachable!(), + }, + ) +} + +fn bar() { + { + { + EvaluateJSReply::NumberValue( + match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) { + Ok(ConversionResult::Success(v)) => v, + _ => unreachable!(), + }, + ) + } + } +}