Add extra tests for match arm placement
This commit is contained in:
parent
2d4a0cbe3b
commit
2eb67827a7
3 changed files with 33 additions and 16 deletions
26
src/expr.rs
26
src/expr.rs
|
@ -918,16 +918,13 @@ impl Rewrite for ast::Arm {
|
||||||
.block_indent(context.config)),
|
.block_indent(context.config)),
|
||||||
body_budget);
|
body_budget);
|
||||||
|
|
||||||
let (body_str, break_line) = try_opt!(match_arm_heuristic(same_line_body.as_ref()
|
let body_str = try_opt!(match_arm_heuristic(same_line_body.as_ref().map(|x| &x[..]),
|
||||||
.map(|x| &x[..]),
|
next_line_body.as_ref().map(|x| &x[..])));
|
||||||
next_line_body.as_ref()
|
|
||||||
.map(|x| &x[..])));
|
|
||||||
|
|
||||||
let spacer = if break_line {
|
let spacer = match same_line_body {
|
||||||
format!("\n{}",
|
Some(ref body) if body == body_str => " ".to_owned(),
|
||||||
offset.block_indent(context.config).to_string(context.config))
|
_ => format!("\n{}",
|
||||||
} else {
|
offset.block_indent(context.config).to_string(context.config)),
|
||||||
" ".to_owned()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(format!("{}{} =>{}{},",
|
Some(format!("{}{} =>{}{},",
|
||||||
|
@ -939,17 +936,14 @@ impl Rewrite for ast::Arm {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes two possible rewrites for the match arm body and chooses the "nicest".
|
// Takes two possible rewrites for the match arm body and chooses the "nicest".
|
||||||
// Bool marks break line or no.
|
fn match_arm_heuristic<'a>(former: Option<&'a str>, latter: Option<&'a str>) -> Option<&'a str> {
|
||||||
fn match_arm_heuristic<'a>(former: Option<&'a str>,
|
|
||||||
latter: Option<&'a str>)
|
|
||||||
-> Option<(&'a str, bool)> {
|
|
||||||
match (former, latter) {
|
match (former, latter) {
|
||||||
(Some(f), None) => Some((f, false)),
|
(f @ Some(..), None) => f,
|
||||||
(Some(f), Some(l)) if f.chars().filter(|&c| c == '\n').count() <=
|
(Some(f), Some(l)) if f.chars().filter(|&c| c == '\n').count() <=
|
||||||
l.chars().filter(|&c| c == '\n').count() => {
|
l.chars().filter(|&c| c == '\n').count() => {
|
||||||
Some((f, false))
|
Some(f)
|
||||||
}
|
}
|
||||||
(_, l) => l.map(|s| (s, true)),
|
(_, l) => l,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -209,3 +209,15 @@ fn issue355() {
|
||||||
dddddddddd),
|
dddddddddd),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue280() {
|
||||||
|
{
|
||||||
|
match x {
|
||||||
|
CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline => ch ==
|
||||||
|
'\n',
|
||||||
|
ast::ItemConst(ref typ, ref expr) => self.process_static_or_const_item(item,
|
||||||
|
&typ,
|
||||||
|
&expr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -199,3 +199,14 @@ fn issue355() {
|
||||||
dddddddddd),
|
dddddddddd),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue280() {
|
||||||
|
{
|
||||||
|
match x {
|
||||||
|
CompressionMode::DiscardNewline | CompressionMode::CompressWhitespaceNewline =>
|
||||||
|
ch == '\n',
|
||||||
|
ast::ItemConst(ref typ, ref expr) =>
|
||||||
|
self.process_static_or_const_item(item, &typ, &expr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue