Repository: intellij-rust/intellij-rust
Issue: Safe cast ignores type alias

<!--
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.157.4174-212
* **Rust toolchain version:** 1.55.0 (c8dfcfe04 2021-09-06) x86_64-pc-windows-msvc
* **IDE name and version:** CLion 2021.2.3 (CL-212.5457.51)
* **Operating system:** Windows 10 10.0
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description

When an integral type is aliased and "add safe cast" suggestion is used, the resulting code does not use the alias.

## Steps to reproduce

```rust
type Foo = i16;

let a: u64 = 1;
let b: u64 = 2;

let c: Foo = b - a; // use add safe case here - ideally the result would be 'as Foo' instead of 'as i16'
```

<!--
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/annotator/fixes/AddAsTyFix.kt, src/test/kotlin/org/rust/ide/annotator/fixes/AddAsTyFixTest.kt

diff --git a/src/main/kotlin/org/rust/ide/annotator/fixes/AddAsTyFix.kt b/src/main/kotlin/org/rust/ide/annotator/fixes/AddAsTyFix.kt
index 157755c1c..89c2a3bec 100644
--- a/src/main/kotlin/org/rust/ide/annotator/fixes/AddAsTyFix.kt
+++ b/src/main/kotlin/org/rust/ide/annotator/fixes/AddAsTyFix.kt
@@ -10,6 +10,8 @@ import com.intellij.openapi.editor.Editor
 import com.intellij.openapi.project.Project
 import com.intellij.psi.PsiElement
 import com.intellij.psi.PsiFile
+import org.rust.ide.presentation.renderInsertionSafe
+import org.rust.ide.presentation.shortPresentableText
 import org.rust.lang.core.psi.RsExpr
 import org.rust.lang.core.psi.RsPsiFactory
 import org.rust.lang.core.types.ty.Ty
@@ -21,10 +23,10 @@ class AddAsTyFix(expr: RsExpr, val ty: Ty) : LocalQuickFixAndIntentionActionOnPs
 
     override fun getFamilyName(): String = "Add safe cast"
 
-    override fun getText(): String = "Add safe cast to $ty"
+    override fun getText(): String = "Add safe cast to ${ty.shortPresentableText}"
 
     override fun invoke(project: Project, file: PsiFile, editor: Editor?, startElement: PsiElement, endElement: PsiElement) {
         if (startElement !is RsExpr) return
-        startElement.replace(RsPsiFactory(project).createCastExpr(startElement, ty.toString()))
+        startElement.replace(RsPsiFactory(project).createCastExpr(startElement, ty.renderInsertionSafe(startElement)))
     }
 }
diff --git a/src/test/kotlin/org/rust/ide/annotator/fixes/AddAsTyFixTest.kt b/src/test/kotlin/org/rust/ide/annotator/fixes/AddAsTyFixTest.kt
index fb1d5164e..0af2e13da 100644
--- a/src/test/kotlin/org/rust/ide/annotator/fixes/AddAsTyFixTest.kt
+++ b/src/test/kotlin/org/rust/ide/annotator/fixes/AddAsTyFixTest.kt
@@ -52,4 +52,18 @@ class AddAsTyFixTest : RsInspectionsTestBase(RsTypeCheckInspection::class) {
             let _: f32 = answer() as f32;
         }
     """)
+
+    fun `test type alias`() = checkFixByText("Add safe cast to Foo", """
+        type Foo = f32;
+        fn answer() -> i32 {42}
+        fn main () {
+            let _: Foo = <error>answer()/*caret*/</error>;
+        }
+    """, """
+        type Foo = f32;
+        fn answer() -> i32 {42}
+        fn main () {
+            let _: Foo = answer() as Foo;
+        }
+    """)
 }