diff --git a/src/formatting/comment.rs b/src/formatting/comment.rs index dba19b12afb..43b03197580 100644 --- a/src/formatting/comment.rs +++ b/src/formatting/comment.rs @@ -1604,7 +1604,15 @@ pub(crate) fn recover_comment_removed( let snippet = context.snippet(span); let includes_comment = contains_comment(snippet); if snippet != new && includes_comment && changed_comment_content(snippet, &new) { - Some(snippet.to_owned()) + /* Trim white spaces at end of lines */ + let mut c = String::from(""); + for line in snippet.to_owned().split('\n') { + if c != "" { + c.push('\n') + }; + c.push_str(line.trim_end()); + } + Some(c.to_owned()) } else { Some(new) } diff --git a/src/formatting/pairs.rs b/src/formatting/pairs.rs index de13597b2c7..3f447a15ab8 100644 --- a/src/formatting/pairs.rs +++ b/src/formatting/pairs.rs @@ -116,6 +116,19 @@ fn rewrite_pairs_multiline( result.push_str(&list.list[0].1.as_ref()?); + /* Determine the expression kind */ + let (e, _s) = match list.list.first() { + Some(x) => x, + None => return None, + }; + let list_len = list.list.len(); + /* ???? Is there better way than transmute() ???? */ + let list_expr = unsafe { std::mem::transmute::<&T, &ast::Expr>(e) }; + let list_expr_lit = match list_expr.kind { + ast::ExprKind::Lit(_) => true, + _ => false, + }; + for ((e, default_rw), s) in list.list[1..].iter().zip(list.separators.iter()) { // The following test checks if we should keep two subexprs on the same // line. We do this if not doing so would create an orphan and there is @@ -125,7 +138,15 @@ fn rewrite_pairs_multiline( } else { shape.used_width() }; - if last_line_width(&result) + offset <= nested_shape.used_width() { + + /* Do not force adding add new line between Lit operation and righ operand */ + let w = if list_len > 2 || !list_expr_lit { + nested_shape.used_width() + } else { + nested_shape.width + }; + + if last_line_width(&result) + offset <= w { // We must snuggle the next line onto the previous line to avoid an orphan. if let Some(line_shape) = shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result)) diff --git a/tests/source/issue-4392.rs b/tests/source/issue-4392.rs new file mode 100644 index 00000000000..6200bc99b97 --- /dev/null +++ b/tests/source/issue-4392.rs @@ -0,0 +1,47 @@ +/* No new line should be added after the operation ('==', etc.) */ + +fn main() { + if 'a' == b /* x */ + as char {} +} + +fn main() { + if 'a' == 0 + /* x */ as char {} } + +fn main() { + if 1234 == 0 + /* x */ & cc & dd {} } + +fn main() { + if 1234 == 'a' + /* x */ as i32 {} } + +fn main() { + if 0 == 'a' + /* x */ as i32 {} } + +fn main() { + if 'a' == b /* x */ as char { println!("Match"); } +} + +fn main() { + while aaaaaa < bbbbb + as char /* x */ {} } + +fn main() { + if 'a' == b // x + as char {} +} + +fn main() { + if 'a' == /* x */ b + as char {} +} + +fn main() { + if 'a' == b + as char {} + + let a = 5; // inline +}