diff --git a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt index 5b2e5187ac..72f6c57254 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt @@ -27,7 +27,24 @@ "trivially_copy_pass_by_ref", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "rust-lang.github.io", + ), + ), + port: None, + path: "/rust-clippy/master/index.html", + query: None, + fragment: Some( + "trivially_copy_pass_by_ref", + ), + }, + }, + ), source: Some( "clippy", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt index 116f0ff734..eb4a6b597b 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt @@ -27,7 +27,24 @@ "E0277", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0277", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt index 2cbf657e59..19f72196da 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt @@ -27,7 +27,24 @@ "E0053", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0053", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt index 1142dc2ac6..15ac95d721 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt @@ -27,7 +27,24 @@ "E0308", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0308", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt index 782c72dbdc..b9650f3e45 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt @@ -27,7 +27,24 @@ "E0061", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "doc.rust-lang.org", + ), + ), + port: None, + path: "/error-index.html", + query: None, + fragment: Some( + "E0061", + ), + }, + }, + ), source: Some( "rustc", ), diff --git a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt index d3f27ab6a5..c45f68a916 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt @@ -27,7 +27,24 @@ "let_and_return", ), ), - code_description: None, + code_description: Some( + CodeDescription { + href: Url { + scheme: "https", + host: Some( + Domain( + "rust-lang.github.io", + ), + ), + port: None, + path: "/rust-clippy/master/index.html", + query: None, + fragment: Some( + "let_and_return", + ), + }, + }, + ), source: Some( "clippy", ), diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index 93bef5c8b4..324019614d 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -211,6 +211,12 @@ pub(crate) fn map_rust_diagnostic_to_lsp( } } + 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() .map(|primary_span| { @@ -248,7 +254,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( range: in_macro_location.range, severity, code: code.clone().map(lsp_types::NumberOrString::String), - code_description: None, + code_description: code_description.clone(), source: Some(source.clone()), message: message.clone(), related_information: Some(information_for_additional_diagnostic), @@ -269,7 +275,7 @@ pub(crate) fn map_rust_diagnostic_to_lsp( range: location.range, severity, code: code.clone().map(lsp_types::NumberOrString::String), - code_description: None, + code_description: code_description.clone(), source: Some(source.clone()), message, related_information: if related_information.is_empty() { @@ -292,6 +298,31 @@ pub(crate) fn map_rust_diagnostic_to_lsp( .collect() } +fn rustc_code_description(code: Option<&str>) -> Option { + 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(|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 { + 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 { diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 118e7276f2..1cf4139d2b 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -1129,7 +1129,14 @@ pub(crate) fn publish_diagnostics( range: to_proto::range(&line_index, d.range), severity: Some(to_proto::diagnostic_severity(d.severity)), code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String), - code_description: None, + code_description: d.code.and_then(|code| { + lsp_types::Url::parse(&format!( + "https://rust-analyzer.github.io/manual.html#{}", + code.as_str() + )) + .ok() + .map(|href| lsp_types::CodeDescription { href }) + }), source: Some("rust-analyzer".to_string()), message: d.message, related_information: None,