From 41e6929862c837877e9c93e52af5f182ef499ee6 Mon Sep 17 00:00:00 2001
From: David Bar-On <david.cdb004@gmail.com>
Date: Wed, 19 Aug 2020 18:52:12 +0300
Subject: [PATCH 1/3] Resolve issue #2896 - trim extra blanks at end of line
 when following comment moved to new line

---
 src/formatting/comment.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

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)
     }

From 5889d2e4be76d25b828397a420d121f6e5734da0 Mon Sep 17 00:00:00 2001
From: David Bar-On <david.cdb004@gmail.com>
Date: Mon, 24 Aug 2020 15:03:14 +0300
Subject: [PATCH 2/3] Changes to resolve issue #4392

---
 src/formatting/pairs.rs | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

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<T: Rewrite>(
 
     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<T: Rewrite>(
         } 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))

From 1e227bf0933d4df188e6e25ea58a5f33586bc4df Mon Sep 17 00:00:00 2001
From: David Bar-On <david.cdb004@gmail.com>
Date: Mon, 24 Aug 2020 15:22:22 +0300
Subject: [PATCH 3/3] Add test cases file for issue #4392

---
 tests/source/issue-4392.rs | 47 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 tests/source/issue-4392.rs

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
+}