Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8feeddf

Browse files
committed
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: rust-lang#3029. Signed-off-by: Otavio Salvador <[email protected]>
1 parent 8b709c0 commit 8feeddf

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/overflow.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syntax::{ast, ptr};
1818
use closures;
1919
use expr::{
2020
can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr,
21+
rewrite_cond,
2122
};
2223
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
2324
use macros::MacroArg;
@@ -403,6 +404,16 @@ impl<'a> Context<'a> {
403404
closures::rewrite_last_closure(self.context, expr, shape)
404405
}
405406
}
407+
ast::ExprKind::Match(..) => {
408+
let multi_line = rewrite_cond(self.context, expr, shape)
409+
.map_or(false, |cond| cond.contains('\n'));
410+
411+
if multi_line {
412+
None
413+
} else {
414+
expr.rewrite(self.context, shape)
415+
}
416+
}
406417
_ => expr.rewrite(self.context, shape),
407418
}
408419
}

tests/source/issue-3029.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn foo() {
2+
EvaluateJSReply::NumberValue(
3+
match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) {
4+
Ok(ConversionResult::Success(v)) => v,
5+
_ => unreachable!(),
6+
},
7+
)
8+
}
9+
10+
fn bar() {
11+
{
12+
{
13+
EvaluateJSReply::NumberValue(
14+
match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) {
15+
Ok(ConversionResult::Success(v)) => v,
16+
_ => unreachable!(),
17+
},
18+
)
19+
}
20+
}
21+
}

tests/target/issue-3029.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fn foo() {
2+
EvaluateJSReply::NumberValue(
3+
match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) {
4+
Ok(ConversionResult::Success(v)) => v,
5+
_ => unreachable!(),
6+
},
7+
)
8+
}
9+
10+
fn bar() {
11+
{
12+
{
13+
EvaluateJSReply::NumberValue(
14+
match FromJSValConvertible::from_jsval(cx, rval.handle(), ()) {
15+
Ok(ConversionResult::Success(v)) => v,
16+
_ => unreachable!(),
17+
},
18+
)
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)