From 2eb67827a7a34c9bb221710763f6d16fb4555a24 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sun, 27 Sep 2015 11:58:26 +0200 Subject: [PATCH] Add extra tests for match arm placement --- src/expr.rs | 28 +++++++++++----------------- tests/source/match.rs | 12 ++++++++++++ tests/target/match.rs | 11 +++++++++++ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index b8a52a02cc7..38305fc7d62 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -918,16 +918,13 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt .block_indent(context.config)), body_budget); - let (body_str, break_line) = try_opt!(match_arm_heuristic(same_line_body.as_ref() - .map(|x| &x[..]), - next_line_body.as_ref() - .map(|x| &x[..]))); - - let spacer = if break_line { - format!("\n{}", - offset.block_indent(context.config).to_string(context.config)) - } else { - " ".to_owned() + let body_str = try_opt!(match_arm_heuristic(same_line_body.as_ref().map(|x| &x[..]), + next_line_body.as_ref().map(|x| &x[..]))); + + let spacer = match same_line_body { + Some(ref body) if body == body_str => " ".to_owned(), + _ => format!("\n{}", + offset.block_indent(context.config).to_string(context.config)), }; Some(format!("{}{} =>{}{},", @@ -939,17 +936,14 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt } // 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, bool)> { +fn match_arm_heuristic<'a>(former: Option<&'a str>, latter: Option<&'a str>) -> Option<&'a str> { 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() <= l.chars().filter(|&c| c == '\n').count() => { - Some((f, false)) + Some(f) } - (_, l) => l.map(|s| (s, true)), + (_, l) => l, } } diff --git a/tests/source/match.rs b/tests/source/match.rs index 9fc32ef1904..49e66e4c0df 100644 --- a/tests/source/match.rs +++ b/tests/source/match.rs @@ -209,3 +209,15 @@ fn issue355() { 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), + } + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index d7227a0da59..6e330505a9b 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -199,3 +199,14 @@ fn issue355() { 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), + } + } +} -- 2.44.0