Repository: intellij-rust/intellij-rust
Issue: invert `if without else` breaks formatting of comments 

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.161.4266-213-nightly
* **Rust toolchain version:** 1.58.0-nightly (547a6ffee 2021-10-21) x86_64-apple-darwin
* **IDE name and version:** CLion 2021.3 RC2 (CL-213.5744.190)
* **Operating system:** macOS 11.4
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description
Plugin formatter does not fix it, see https://github.com/intellij-rust/intellij-rust/issues/8168.
Btw, invert `if with else` works fine with such an unnatural amount of comments :)


## Steps to reproduce
```rust
fn a(c: bool) -> i32 {
    // a
    if c {   //b
        // c
        println!("200"); // d
        return 50; // e
    } //f
    // g
    println!("100"); // h
    70 // i
}

```
invert if

## Result
```rust
fn a(c: bool) -> i32 {
    // a
    if !c {   //f
// g
        println!("100"); // h
        return 70; // i
    } //b
// c
    println!("200"); // d
    50 // e
}
``` 

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->

Changed files:
src/main/kotlin/org/rust/ide/intentions/InvertIfIntention.kt, src/test/kotlin/org/rust/ide/intentions/InvertIfIntentionTest.kt

diff --git a/src/main/kotlin/org/rust/ide/intentions/InvertIfIntention.kt b/src/main/kotlin/org/rust/ide/intentions/InvertIfIntention.kt
index 964a007c4..a64e1ce0b 100644
--- a/src/main/kotlin/org/rust/ide/intentions/InvertIfIntention.kt
+++ b/src/main/kotlin/org/rust/ide/intentions/InvertIfIntention.kt
@@ -352,10 +352,11 @@ private fun RsBlock.deleteContinuousChildRange(stmts: List<PsiElement>) {
 }
 
 private fun RsElement.addAllAfter(elements: List<PsiElement>, anchor: PsiElement) {
-    val anchorBefore = anchor.nextSibling
-    for (element in elements) {
-        // Can't use `addAfter` since it returns incorrect element when adding whitespace
-        addBefore(element, anchorBefore)
+    for (element in elements.asReversed()) {
+        // Note that:
+        // - return value of `addAfter` is incorrect when adding whitespace
+        // - `addBefore` doesn't work when adding whitespace after comment
+        addAfter(element, anchor)
     }
 }
 
diff --git a/src/test/kotlin/org/rust/ide/intentions/InvertIfIntentionTest.kt b/src/test/kotlin/org/rust/ide/intentions/InvertIfIntentionTest.kt
index 2b22dd3f8..3994aee6d 100644
--- a/src/test/kotlin/org/rust/ide/intentions/InvertIfIntentionTest.kt
+++ b/src/test/kotlin/org/rust/ide/intentions/InvertIfIntentionTest.kt
@@ -428,4 +428,30 @@ class InvertIfIntentionTest : RsIntentionTestBase(InvertIfIntention::class) {
             }
         }
     """)
+
+    fun `test if without else comments`() = doAvailableTest("""
+        fn func(f: bool) -> i32 {
+            // a
+            /*caret*/if f {   // b
+                // c
+                println!("1"); // d
+                return 1; // e
+            } // f
+            // g
+            println!("2"); // h
+            2 // i
+        }
+    """, """
+        fn func(f: bool) -> i32 {
+            // a
+            /*caret*/if !f {   // f
+                // g
+                println!("2"); // h
+                return 2; // i
+            } // b
+            // c
+            println!("1"); // d
+            1 // e
+        }
+    """)
 }