Repository: intellij-rust/intellij-rust
Issue: Broken remove redundant type arguments

<!--
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.3.138.3572-203
* **Rust toolchain version:** 1.51.0-nightly (fde692739 2021-01-02) x86_64-pc-windows-msvc
* **IDE name and version:** CLion 2020.3.1 (CL-203.6682.181)
* **Operating system:** Windows 10 10.0
* **Macro expansion engine:** new
* **Name resolution engine:** old

## Problem description
```rust
struct B<'a, T>;

struct C<'a> {
    a: B<'a, u32, u32>
}
```
It fails to remove redundant type arguments

## Steps to reproduce
1. Place aforementioned code in editor
2. Attempt to `remove redundant type arguments`

<!--
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/test/kotlin/org/rust/ide/inspections/RsWrongTypeArgumentsNumberInspectionTest.kt
src/main/kotlin/org/rust/ide/inspections/fixes/RemoveTypeArguments.kt

diff --git a/src/main/kotlin/org/rust/ide/inspections/fixes/RemoveTypeArguments.kt b/src/main/kotlin/org/rust/ide/inspections/fixes/RemoveTypeArguments.kt
index 5e85132e7..0ad39ba41 100644
--- a/src/main/kotlin/org/rust/ide/inspections/fixes/RemoveTypeArguments.kt
+++ b/src/main/kotlin/org/rust/ide/inspections/fixes/RemoveTypeArguments.kt
@@ -10,6 +10,8 @@ import com.intellij.codeInspection.ProblemDescriptor
 import com.intellij.openapi.project.Project
 import org.rust.lang.core.psi.*
 import org.rust.lang.core.psi.ext.RsElement
+import org.rust.lang.core.psi.ext.deleteWithSurroundingComma
+import org.rust.lang.core.psi.ext.getNextNonCommentSibling
 
 class RemoveTypeArguments(
     private val startIndex: Int,
@@ -32,14 +34,13 @@ class RemoveTypeArguments(
     }
 
     private fun RsTypeArgumentList.removeTypeParameters() {
-        if (lifetimeList.size > 0) return
-
-        if (startIndex == 0) delete()
-        else {
-            val startElement = typeReferenceList[startIndex - 1].nextSibling
-            val endElement = typeReferenceList[endIndex - 1]
-
-            deleteChildRange(startElement, endElement)
+        val count = endIndex - startIndex
+        for (i in 0 until count) {
+            typeReferenceList[startIndex].deleteWithSurroundingComma()
+        }
+        // If the type argument list is empty, delete it
+        if (lt.getNextNonCommentSibling() == gt) {
+            delete()
         }
     }
 }
diff --git a/src/test/kotlin/org/rust/ide/inspections/RsWrongTypeArgumentsNumberInspectionTest.kt b/src/test/kotlin/org/rust/ide/inspections/RsWrongTypeArgumentsNumberInspectionTest.kt
index 29bc3a03c..1d21877cf 100644
--- a/src/test/kotlin/org/rust/ide/inspections/RsWrongTypeArgumentsNumberInspectionTest.kt
+++ b/src/test/kotlin/org/rust/ide/inspections/RsWrongTypeArgumentsNumberInspectionTest.kt
@@ -391,4 +391,32 @@ class RsWrongTypeArgumentsNumberInspectionTest : RsInspectionsTestBase(RsWrongTy
             let x: foo::   S<T>;
         }
     """)
+
+    fun `test remove type arguments with lifetime 1`() = checkFixByText("Remove redundant type arguments", """
+        struct B<'a, T>(&'a T);
+
+        struct C<'a> {
+            a: <error descr="Wrong number of type arguments: expected 1, found 2 [E0107]">B<'a, u32, i32>/*caret*/</error>
+        }
+    """, """
+        struct B<'a, T>(&'a T);
+
+        struct C<'a> {
+            a: B<'a, u32>
+        }
+    """)
+
+    fun `test remove type arguments with lifetime 2`() = checkFixByText("Remove redundant type arguments", """
+        struct B<'a>(&'a u32);
+
+        struct C<'a> {
+            a: <error descr="Wrong number of type arguments: expected 0, found 1 [E0107]">B<'a, i32>/*caret*/</error>
+        }
+    """, """
+        struct B<'a>(&'a u32);
+
+        struct C<'a> {
+            a: B<'a>
+        }
+    """)
 }