Repository: intellij-rust/intellij-rust
Issue: Make reference mut fix removes lifetime of reference


## Environment

* **Intellij-Rust plugin version:** v0.2.0.2112-183
* **Rust toolchain version:** 1.31
* **IDE name and version:** Clion
* **Operating system:** Mac

## Problem description

Make reference mut fix removes lifetime of reference.
Example:
```rust
struct A {}

impl A {
    fn foo(&mut self) {  }

    fn bar<'a>(&self, obj: &'a A) {
        obj.foo()
    }
}
```
After applying fix:
```rust
struct A {}

impl A {
    fn foo(&mut self) {  }

    fn bar<'a>(&self, obj: &mut A) {
        obj.foo()
    }
}
```


Changed files:
src/main/kotlin/org/rust/ide/annotator/fixes/AddMutableFix.kt, src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt, src/test/kotlin/org/rust/ide/annotator/fixes/AddMutableFixText.kt

diff --git a/src/main/kotlin/org/rust/ide/annotator/fixes/AddMutableFix.kt b/src/main/kotlin/org/rust/ide/annotator/fixes/AddMutableFix.kt
index acfe5661e..7f7327e16 100644
--- a/src/main/kotlin/org/rust/ide/annotator/fixes/AddMutableFix.kt
+++ b/src/main/kotlin/org/rust/ide/annotator/fixes/AddMutableFix.kt
@@ -54,7 +54,7 @@ fun updateMutable(project: Project, binding: RsNamedElement, mutable: Boolean =
             val type = parameter?.typeReference?.typeElement
             if (type is RsRefLikeType) {
                 val newParameterExpr = RsPsiFactory(project)
-                    .createValueParameter(parameter.pat?.text!!, type.typeReference, mutable)
+                    .createValueParameter(parameter.pat?.text!!, type.typeReference, mutable, type.lifetime)
                 parameter.replace(newParameterExpr)
                 return
             }
diff --git a/src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt b/src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt
index 467941cce..a90af9185 100644
--- a/src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt
+++ b/src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt
@@ -286,8 +286,8 @@ class RsPsiFactory(private val project: Project) {
         createFromText<RsImplItem>("impl $name {\\n${functions.joinToString(separator = "\\n", transform = { it.text })}\\n}")
             ?: error("Failed to create RsImplItem element")
 
-    fun createValueParameter(name: String, type: RsTypeReference, mutable: Boolean = false): RsValueParameter {
-        return createFromText<RsFunction>("fn main($name: ${if (mutable) "&mut " else ""}${type.text}){}")
+    fun createValueParameter(name: String, type: RsTypeReference, mutable: Boolean = false, lifetime: RsLifetime? = null): RsValueParameter {
+        return createFromText<RsFunction>("fn main($name: &${if (lifetime != null) lifetime.text + " " else ""}${if (mutable) "mut " else ""}${type.text}){}")
             ?.valueParameterList?.valueParameterList?.get(0)
             ?: error("Failed to create parameter element")
     }
diff --git a/src/test/kotlin/org/rust/ide/annotator/fixes/AddMutableFixText.kt b/src/test/kotlin/org/rust/ide/annotator/fixes/AddMutableFixText.kt
index 42878ecd7..a7364f8d5 100644
--- a/src/test/kotlin/org/rust/ide/annotator/fixes/AddMutableFixText.kt
+++ b/src/test/kotlin/org/rust/ide/annotator/fixes/AddMutableFixText.kt
@@ -65,4 +65,26 @@ class AddMutableFixText : RsInspectionsTestBase(RsBorrowCheckerInspection()) {
             }
         }
     """)
+
+    fun `test add mutable fix to parameter with lifetime`() = checkFixByText("Make `obj` mutable", """
+        struct A {}
+
+        impl A {
+            fn foo(&mut self) {  }
+
+            fn bar<'a>(&self, obj: &'a A) {
+                <error>obj/*caret*/</error>.foo()
+            }
+        }
+    """, """
+        struct A {}
+
+        impl A {
+            fn foo(&mut self) {  }
+
+            fn bar<'a>(&self, obj: &'a mut A) {
+                obj.foo()
+            }
+        }
+    """)
 }