diff --git a/src/matches.rs b/src/matches.rs index d46b19be723..87bce7fab04 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -289,7 +289,13 @@ fn rewrite_match_pattern( let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?; // Guard - let guard_str = rewrite_guard(context, guard, shape, trimmed_last_line_width(&pats_str))?; + let guard_str = rewrite_guard( + context, + guard, + shape, + trimmed_last_line_width(&pats_str), + pats_str.contains("\n"), + )?; Some(format!("{}{}", pats_str, guard_str)) } @@ -450,6 +456,7 @@ fn rewrite_guard( // The amount of space used up on this line for the pattern in // the arm (excludes offset). pattern_width: usize, + multiline_pattern: bool, ) -> Option { if let Some(ref guard) = *guard { // First try to fit the guard string on the same line as the pattern. @@ -457,10 +464,12 @@ fn rewrite_guard( let cond_shape = shape .offset_left(pattern_width + 4) .and_then(|s| s.sub_width(5)); - if let Some(cond_shape) = cond_shape { - if let Some(cond_str) = guard.rewrite(context, cond_shape) { - if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() { - return Some(format!(" if {}", cond_str)); + if !multiline_pattern { + if let Some(cond_shape) = cond_shape { + if let Some(cond_str) = guard.rewrite(context, cond_shape) { + if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() { + return Some(format!(" if {}", cond_str)); + } } } } diff --git a/tests/source/match.rs b/tests/source/match.rs index 244d0b45f25..8ff7afb7a44 100644 --- a/tests/source/match.rs +++ b/tests/source/match.rs @@ -491,3 +491,19 @@ fn issue_2621() { | Foo::I => println!("With comment"), // Comment after line } } + +fn issue_2377() { + match tok { + Tok::Not + | Tok::BNot + | Tok::Plus + | Tok::Minus + | Tok::PlusPlus + | Tok::MinusMinus + | Tok::Void + | Tok::Delete if prec <= 16 => { + // code here... + } + Tok::TypeOf if prec <= 16 => {} + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index be1ecb49535..cda0ed2abe5 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -518,3 +518,21 @@ fn issue_2621() { Foo::I => println!("With comment"), // Comment after line } } + +fn issue_2377() { + match tok { + Tok::Not + | Tok::BNot + | Tok::Plus + | Tok::Minus + | Tok::PlusPlus + | Tok::MinusMinus + | Tok::Void + | Tok::Delete + if prec <= 16 => + { + // code here... + } + Tok::TypeOf if prec <= 16 => {} + } +}