]> git.lizzy.rs Git - rust.git/commitdiff
Link clippy lint codes in diagnostics
authorLukas Wirth <lukastw97@gmail.com>
Tue, 17 Nov 2020 20:56:37 +0000 (21:56 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Wed, 18 Nov 2020 07:50:27 +0000 (08:50 +0100)
crates/rust-analyzer/src/diagnostics/to_proto.rs

index d3720de33e5db9ab8fbb0f08684bcb2b28de44db..324019614d528ab7ffe8d4b23845e4c3cb388c3e 100644 (file)
@@ -2,7 +2,7 @@
 //! `cargo check` json format to the LSP diagnostic format.
 use std::{collections::HashMap, path::Path};
 
-use flycheck::{Applicability, DiagnosticCode, DiagnosticLevel, DiagnosticSpan};
+use flycheck::{Applicability, DiagnosticLevel, DiagnosticSpan};
 use stdx::format_to;
 
 use crate::{lsp_ext, to_proto::url_from_abs_path};
@@ -211,7 +211,11 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
         }
     }
 
-    let code_description = rustc_code_description(rd.code.as_ref());
+    let code_description = match source.as_str() {
+        "rustc" => rustc_code_description(code.as_deref()),
+        "clippy" => clippy_code_description(code.as_deref()),
+        _ => None,
+    };
 
     primary_spans
         .iter()
@@ -294,20 +298,31 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
         .collect()
 }
 
-fn rustc_code_description(code: Option<&DiagnosticCode>) -> Option<lsp_types::CodeDescription> {
-    code.filter(|c| {
-        let mut chars = c.code.chars();
+fn rustc_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
+    code.filter(|code| {
+        let mut chars = code.chars();
         chars.next().map_or(false, |c| c == 'E')
             && chars.by_ref().take(4).all(|c| c.is_ascii_digit())
             && chars.next().is_none()
     })
-    .and_then(|c| {
-        lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", c.code))
+    .and_then(|code| {
+        lsp_types::Url::parse(&format!("https://doc.rust-lang.org/error-index.html#{}", code))
             .ok()
             .map(|href| lsp_types::CodeDescription { href })
     })
 }
 
+fn clippy_code_description(code: Option<&str>) -> Option<lsp_types::CodeDescription> {
+    code.and_then(|code| {
+        lsp_types::Url::parse(&format!(
+            "https://rust-lang.github.io/rust-clippy/master/index.html#{}",
+            code
+        ))
+        .ok()
+        .map(|href| lsp_types::CodeDescription { href })
+    })
+}
+
 #[cfg(test)]
 #[cfg(not(windows))]
 mod tests {