Repository: intellij-rust/intellij-rust
Issue: Error index (Exxxx) links not functional

<!--
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.
Consider using "Help | Create New Issue" action that fills out all necessary information automatically.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.172.4687-222
* **Rust toolchain version:** rustup 1.24.3 (ce5817a94 2021-05-31)
* **IDE name and version:** Build #IU-222.2964.55, built on June 9, 2022
* **Operating system:** Windows 11 Pro 22H2

## Problem description

When hovering over an error in the editor, the popup shows a link to the related compiler error (for example E0716), which is blue and underlined. However, clicking on the link doesn't work (nothing happens). 

## Steps to reproduce

1. Write some rust code with an error
2. Hover over the red-underlined section
3. Click on the suggested error index link

<!--
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/).

If you provide a screenshot of some code, please also copy-paste the corresponding code snippet here.
-->


Changed files:
src/test/kotlin/org/rustSlowTests/RsExternalLinterPassTest.kt, src/main/kotlin/org/rust/ide/annotator/RsExternalLinterUtils.kt

diff --git a/src/main/kotlin/org/rust/ide/annotator/RsExternalLinterUtils.kt b/src/main/kotlin/org/rust/ide/annotator/RsExternalLinterUtils.kt
index 84944fa98..553ee7aba 100644
--- a/src/main/kotlin/org/rust/ide/annotator/RsExternalLinterUtils.kt
+++ b/src/main/kotlin/org/rust/ide/annotator/RsExternalLinterUtils.kt
@@ -248,21 +248,25 @@ private data class RsExternalLinterFilteredMessage(
 
             val textRange = span.toTextRange(document) ?: return null
 
-            val tooltip = with(ArrayList<String>()) {
+            val tooltip = buildString {
+                append(formatMessage(StringEscapeUtils.escapeHtml(message.message)).escapeUrls())
                 val code = message.code.formatAsLink()
-                add(StringEscapeUtils.escapeHtml(message.message) + if (code == null) "" else " $code")
-
-                if (span.label != null && !message.message.startsWith(span.label)) {
-                    add(StringEscapeUtils.escapeHtml(span.label))
+                if (code != null) {
+                    append(" [$code]")
                 }
 
-                message.children
-                    .filter { it.message.isNotBlank() }
-                    .map { "${it.level.capitalize()}: ${StringEscapeUtils.escapeHtml(it.message)}" }
-                    .forEach { add(it) }
+                with(mutableListOf<String>()) {
+                    if (span.label != null && !message.message.startsWith(span.label)) {
+                        add(StringEscapeUtils.escapeHtml(span.label))
+                    }
+
+                    message.children
+                        .filter { it.message.isNotBlank() }
+                        .map { "${it.level.capitalize()}: ${StringEscapeUtils.escapeHtml(it.message)}" }
+                        .forEach { add(it) }
 
-                joinToString("<br>") { formatMessage(it) }
-                    .replace(URL_REGEX) { url -> "<a href='${url.value}'>${url.value}</a>" }
+                    append(joinToString(prefix = "<br>", separator = "<br>") { formatMessage(it) }.escapeUrls())
+                }
             }
 
             return RsExternalLinterFilteredMessage(
@@ -276,6 +280,8 @@ private data class RsExternalLinterFilteredMessage(
     }
 }
 
+private fun String.escapeUrls(): String = replace(URL_REGEX) { url -> "<a href='${url.value}'>${url.value}</a>" }
+
 fun RustcSpan.isValid(): Boolean =
     line_end > line_start || (line_end == line_start && column_end >= column_start)
 
diff --git a/src/test/kotlin/org/rustSlowTests/RsExternalLinterPassTest.kt b/src/test/kotlin/org/rustSlowTests/RsExternalLinterPassTest.kt
index a25993ee5..b7eb90216 100644
--- a/src/test/kotlin/org/rustSlowTests/RsExternalLinterPassTest.kt
+++ b/src/test/kotlin/org/rustSlowTests/RsExternalLinterPassTest.kt
@@ -168,7 +168,7 @@ class RsExternalLinterPassTest : RsWithToolchainTestBase() {
             let _: () = 0;
         }
     """, """
-        mismatched types <a href="<a href='https://doc.rust-lang.org/error-index.html#E0308'>https://doc.rust-lang.org/error-index.html#E0308</a>">E0308</a><br>expected `()`, found integer
+        mismatched types [<a href="https://doc.rust-lang.org/error-index.html#E0308">E0308</a>]<br>expected `()`, found integer
     """)
 
     @MinRustcVersion("1.44.1")