Merge pull request #1032 from marcusklaas/else-if-let-overflow

Fix constraints on pattern formatting of else arms
This commit is contained in:
Nick Cameron 2016-06-05 18:05:19 +01:00
commit e7294285f0
4 changed files with 63 additions and 23 deletions

View file

@ -29,7 +29,8 @@ pub fn rewrite_comment(orig: &str,
let s = orig.trim(); let s = orig.trim();
// Edge case: block comments. Let's not trim their lines (for now). // Edge case: block comments. Let's not trim their lines (for now).
let (opener, closer, line_start) = if block_style { let (opener, closer, line_start) =
if block_style {
("/* ", " */", " * ") ("/* ", " */", " * ")
} else if !config.normalize_comments { } else if !config.normalize_comments {
if orig.starts_with("/**") { if orig.starts_with("/**") {
@ -127,7 +128,8 @@ fn left_trim_comment_line(line: &str) -> &str {
line.starts_with("/** ") { line.starts_with("/** ") {
&line[4..] &line[4..]
} else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") || } else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") ||
line.starts_with("///") || line.starts_with("** ") || line.starts_with("/*!") || line.starts_with("///") ||
line.starts_with("** ") || line.starts_with("/*!") ||
line.starts_with("/**") { line.starts_with("/**") {
&line[3..] &line[3..]
} else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//") || } else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//") ||

View file

@ -725,6 +725,14 @@ fn rewrite_if_else(context: &RewriteContext,
offset: Indent, offset: Indent,
allow_single_line: bool) allow_single_line: bool)
-> Option<String> { -> Option<String> {
let (budget, indent) = if !allow_single_line {
// We are part of an if-elseif-else chain. Our constraints are tightened.
// 7 = "} else" .len()
(try_opt!(width.checked_sub(7)), offset + 7)
} else {
(width, offset)
};
// 3 = "if ", 2 = " {" // 3 = "if ", 2 = " {"
let pat_penalty = match context.config.else_if_brace_style { let pat_penalty = match context.config.else_if_brace_style {
ElseIfBraceStyle::AlwaysNextLine => 3, ElseIfBraceStyle::AlwaysNextLine => 3,
@ -735,8 +743,8 @@ fn rewrite_if_else(context: &RewriteContext,
cond, cond,
"let ", "let ",
" =", " =",
try_opt!(width.checked_sub(pat_penalty)), try_opt!(budget.checked_sub(pat_penalty)),
offset + 3)); indent + 3));
// Try to format if-else on single line. // Try to format if-else on single line.
if expr_type == ExprType::SubExpression && allow_single_line && if expr_type == ExprType::SubExpression && allow_single_line &&
@ -778,6 +786,8 @@ fn rewrite_if_else(context: &RewriteContext,
let rewrite = match else_block.node { let rewrite = match else_block.node {
// If the else expression is another if-else expression, prevent it // If the else expression is another if-else expression, prevent it
// from being formatted on a single line. // from being formatted on a single line.
// Note how we're passing the original width and offset, as the
// cost of "else" should not cascade.
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => { ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => {
rewrite_if_else(context, rewrite_if_else(context,
cond, cond,

View file

@ -272,3 +272,16 @@ fn if_else() {
-1.0 -1.0
}; };
} }
fn complex_if_else() {
if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
} else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
ha();
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
yo();
} else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
ha();
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxx {
yo();
}
}

View file

@ -275,3 +275,18 @@ fn if_else() {
let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 }; let cx = tp1.x + any * radius * if anticlockwise { 1.0 } else { -1.0 };
} }
fn complex_if_else() {
if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
} else if let Some(x) = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
ha();
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
yo();
} else if let Some(x) =
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
ha();
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
xxxxxxxxx {
yo();
}
}